//supplyAsync 和 runAsync 区别在前个有返回值,后一个没有返回值
public static void supplyAsync() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
// executorService.submit(() -> {
// System.out.println("executorService 是否为守护线程 :" + Thread.currentThread().isDaemon());
// return null;
// });
CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
System.out.println("this is lambda supplyAsync");
System.out.println("supplyAsync 是否为守护线程 " + Thread.currentThread().isDaemon());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result1";
});
CompletableFuture<String> completableFuture2=CompletableFuture.supplyAsync(()->{
System.out.println("this is executorService");
System.out.println("executorService supplyAsync 是否为守护线程 " + Thread.currentThread().isDaemon());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result2";
},executorService);
System.out.println(completableFuture1.get());
System.out.println(completableFuture2.get());
executorService.shutdown();
}
//这两个方法的入参是一个completableFuture组、allOf就是所有任务都完成时返回。但是是个Void的返回值。
//anyOf是当入参的completableFuture组中有一个任务执行完毕就返回。返回结果是第一个完成的任务的结果
public static void of() throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result1";
});
CompletableFuture<String> completableFuture2=CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result2";
});
CompletableFuture allOf = CompletableFuture.allOf(completableFuture1,completableFuture2);
System.out.println(allOf.get());
CompletableFuture anyOf = CompletableFuture.anyOf(completableFuture1,completableFuture2);
System.out.println(anyOf.get());
}
//这个方法是执行这个方法的时候任务执行完了就返回任务的结果,如果任务没有执行完就返回你的入参。
public static void getNow() throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "result1";
});
System.out.println(completableFuture1.getNow("222"));//输出:222
}
/*
whenComplete:如果调用whenComplete的中途,还发生了其他事情,图中的主线程的sleep(400);导致completableFuture这个任务执行完毕了,
那么就使用主线程调用。如果调用的中途没有发生其他任务且在触碰到whenComplete方法时completableFuture这个任务还没有彻底执行完毕那么就会用completableFuture这个任务所使用的线程。
whenCompleteAsync: 异步执行
*/
public static void whenXXX() throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
return "result1";
}).whenCompleteAsync((result,ex)->{
System.out.println(result+"-"+ex+"-"+Thread.currentThread());
});
System.out.println("-"+Thread.currentThread());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(completableFuture1.get()+"-"+Thread.currentThread());
}
/*
使用任务的线程接着执行后续的操作
thenCompose: 上一个任务结果是then的一部分
thenCombine: 2个任务的结果合并到一个方法里
thenApply: 有入参有返回值类型的
thenAccept : 有入参没有返回值类型
*/
public static void thenXXX() throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread());
return "result1";
});
CompletableFuture<String> thenCompose=completableFuture1.thenCompose(s -> CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(s+"-"+Thread.currentThread());
return s+":"+"thenCompose";
}));//s=="result1"
CompletableFuture<String> thenCombine=completableFuture1.thenCombine( CompletableFuture.supplyAsync(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thenCombine"+"-"+Thread.currentThread());
return "thenCombine";
}),(s1,s2)-> s1+":"+s2 );//2个任务的结果合并到一个方法里
System.out.println(completableFuture1.get());
System.out.println(thenCompose.get());
System.out.println(thenCombine.get());
}
CompletableFuture学习
最新推荐文章于 2024-08-07 16:52:30 发布