public static void supplyAsync() throws ExecutionException
, InterruptedException
{
ExecutorService executorService
= Executors
.newCachedThreadPool();
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();
}
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"));
}
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());
}
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";
}));
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
);
System
.out
.println(completableFuture1
.get());
System
.out
.println(thenCompose
.get());
System
.out
.println(thenCombine
.get());
}
转载请注明原文地址:https://tech.qufami.com/read-23495.html