异步方法
This commit is contained in:
parent
e69747d335
commit
3bdc11c429
|
@ -1,5 +1,6 @@
|
|||
HELP.md
|
||||
target/
|
||||
out/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class CallableWithFutureTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (ExecutorService executorService = Executors.newSingleThreadExecutor()) {
|
||||
Callable<Integer> task = () -> {
|
||||
Thread.sleep(1000);
|
||||
return 42;
|
||||
};
|
||||
Future<Integer> future = executorService.submit(task);
|
||||
|
||||
// 是否完成
|
||||
boolean done = future.isDone();
|
||||
System.out.println("是否完成:" + done);
|
||||
|
||||
// 等待任务执行完成并获取结果,方法会阻塞,直到等到结果或者结果超时
|
||||
Integer result = future.get();
|
||||
|
||||
// 取消操作
|
||||
boolean cancel = future.cancel(true);
|
||||
System.out.println("是否取消:" + cancel);
|
||||
|
||||
// 是否在任务完成前取消,如果是返回true
|
||||
boolean cancelled = future.isCancelled();
|
||||
System.out.println("是否在任务完成前取消:" + cancelled);
|
||||
|
||||
System.out.println("任务结果: " + result);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest1 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
|
||||
// 模拟一个耗时的计算任务
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return 42;
|
||||
});
|
||||
|
||||
future.thenAccept(result -> {
|
||||
System.out.println("任务结果1: " + result);
|
||||
});
|
||||
|
||||
// 等待所有任务完成
|
||||
CompletableFuture.allOf(future).join();
|
||||
|
||||
// 返回结果为void
|
||||
future.thenAccept(result -> System.out.println("任务结果2: " + result));
|
||||
|
||||
// thenApply 的链式调用
|
||||
CompletableFuture<String> thenApply = future.thenApply(result -> {
|
||||
System.out.println("任务结果3: " + result);
|
||||
return "返回内容";
|
||||
}).thenApply(result -> {
|
||||
System.out.println("继续调用,看下之前结果:" + result);
|
||||
return "最后一次返回";
|
||||
});
|
||||
System.out.println(thenApply.get());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest2 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
|
||||
|
||||
// 传入所有的任务返回(CompletableFuture)
|
||||
// 等待所有任务都完成
|
||||
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2);
|
||||
|
||||
// 阻塞线程
|
||||
allFutures.join();
|
||||
System.out.println("所有任务都完成了。。。");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest3 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
|
||||
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
|
||||
|
||||
// 可以传入多个任务(CompletableFuture)
|
||||
// 任意一个任务完成
|
||||
CompletableFuture<Object> anyFuture = CompletableFuture.anyOf(future1, future2);
|
||||
|
||||
// 阻塞获取结果
|
||||
Object result = anyFuture.join();
|
||||
System.out.println("任意一个任务完成: " + result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest4 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
|
||||
|
||||
// 链式调用和JavaScript中promise有点像
|
||||
CompletableFuture<String> transformedFuture = future
|
||||
.thenApply(result -> "Result1: " + result)
|
||||
.thenApply(result -> "Result2: " + result);
|
||||
|
||||
String result = transformedFuture.join();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest5 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "对");
|
||||
|
||||
CompletableFuture<Void> consumedFuture = future
|
||||
.thenAccept(result -> System.out.println("我说的对吧: " + result))
|
||||
.thenAccept(result -> System.out.println("Result2: " + result))
|
||||
.thenAccept(result -> System.out.println("这个也可以链式调用,只是没有返回值" + result));
|
||||
|
||||
consumedFuture.join();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest6 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
|
||||
|
||||
// 对结果调用函数并执行返回 Future
|
||||
CompletableFuture<String> composedFuture = future
|
||||
.thenCompose(result -> CompletableFuture.supplyAsync(() -> "Result: " + result));
|
||||
|
||||
// 拿到返回值
|
||||
String result = composedFuture.join();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest7 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
|
||||
// 模拟一个抛出异常的任务
|
||||
throw new RuntimeException("Task failed");
|
||||
});
|
||||
|
||||
CompletableFuture<Integer> handledFuture = future.exceptionally(ex -> {
|
||||
System.out.println("Exception 消息是什么呢?: " + ex.getMessage());
|
||||
return 0;
|
||||
});
|
||||
|
||||
|
||||
int result = handledFuture.join();
|
||||
System.out.println("Result: " + result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class CompletableFutureTest8 {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture
|
||||
.completedFuture("https://leetcode.cn/problemset/algorithms/?difficulty=EASY&page=1&status=NOT_STARTED&sorting=W3sic29ydE9yZGVyIjoiQVNDRU5ESU5HIiwib3JkZXJCeSI6IkZST05URU5EX0lEIn1d")
|
||||
.thenApply(url -> {
|
||||
try {
|
||||
HttpRequest httpRequest = HttpRequest.newBuilder().GET().uri(new URI(url)).build();
|
||||
System.out.println("httpRequest构建完成。。。");
|
||||
HttpClient client = HttpClient.newBuilder().build();
|
||||
|
||||
return client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.thenAccept(response -> {
|
||||
System.out.println(response.body());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class FutureTaskTest1 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
FutureTask<Integer> futureTask = new FutureTask<>(() -> {
|
||||
// 模拟一个耗时的计算任务
|
||||
Thread.sleep(1000);
|
||||
return 42;
|
||||
});
|
||||
|
||||
Thread thread = new Thread(futureTask);
|
||||
thread.start();
|
||||
|
||||
// 等待任务执行完成并获取结果
|
||||
Integer result = futureTask.get();
|
||||
|
||||
System.out.println("任务结果: " + result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package bunny.async;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class FutureTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (ExecutorService executorService = Executors.newSingleThreadExecutor()) {
|
||||
Future<Integer> future = executorService.submit(() -> {
|
||||
// 模拟一个耗时的计算任务
|
||||
Thread.sleep(1000);
|
||||
return 42;
|
||||
});
|
||||
|
||||
// 检查任务是否完成
|
||||
if (future.isDone()) {
|
||||
// 等待任务执行完成并获取结果
|
||||
Integer result = future.get();
|
||||
System.out.println("任务结果: " + result);
|
||||
} else {
|
||||
System.out.println("任务尚未完成");
|
||||
}
|
||||
|
||||
Integer result = future.get();
|
||||
System.out.println("任务结果: " + result);
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package bunny.queue;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
|
||||
public class QueueArrayBlockingQueueTest1 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ArrayBlockingQueue<Integer> integers = new ArrayBlockingQueue<>(10);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
boolean success = integers.offer(i);
|
||||
if (!success) {
|
||||
System.out.println("队列已满,无法插入元素:" + i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
integers.take();
|
||||
}
|
||||
|
||||
System.out.println(integers);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package bunny.queue;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
public class QueueLinkedBlockingDequeTest {
|
||||
public static void main(String[] args) {
|
||||
LinkedBlockingDeque<Integer> integers = new LinkedBlockingDeque<>();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package bunny.queue;
|
||||
|
||||
public class QueuePriorityBlockingQueueTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue