总的来说,就是
CompletableFuture future = CompletableFuture.runAsync(()->{
System.out.println("hello world");
});
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {
@Override
public String get() {
return "Hello";
}
});
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
supplyAsync 和 runAsync 的区别是supplyAsyncy有返回值,而runAsync没有返回值。
CompletableFuture future = CompletableFuture.supplyAsync(()->"Hello");
// 多次重复调会失效
future.complete("World");
future.complete("World2");
future.complete("World3");
future.complete("World4");
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
如果future已经执行完毕能够返回结果,此时再调complete(T t)则会无效
CompletableFuture future = CompletableFuture.supplyAsync(()->"Hello");
//如果future已经执行完毕能够返回结果,此时再调complete(T t)则会无效
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
CompletableFuture future = CompletableFuture.supplyAsync(()->"Hello").thenApply(s->s+" world").thenApply(String::toUpperCase);
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
CompletableFuture future = CompletableFuture.supplyAsync(()->"Hello").thenCompose(s->CompletableFuture.supplyAsync(()->s + " world"));
//thenCompose 可以用于组合多个CompletableFuture,将前一个结果作为下一个计算的参数,它们之间存在着先后顺序
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
theApply 强调的是类型转换,而thenCompose强调的是执行顺序,就是前一个计算结果作为下一个计算的参数。
CompletableFuture future1 = CompletableFuture.supplyAsync(()->"100");
CompletableFuture future2 = CompletableFuture.supplyAsync(()->10);
CompletableFuture future = future1.thenCombine(future2,(s,i)->Double.parseDouble(s+i));
// 使用thenCombine是将future1 和future2的结果汇总,这一点跟thenCompose()不同。其中future1和future2是并行执行的。
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
CompletableFuture future1 = CompletableFuture.supplyAsync(()->"100");
CompletableFuture future2 = CompletableFuture.supplyAsync(()->70);
CompletableFuture future = future1.thenAcceptBoth(future2,(s,i)->{
System.out.println(s+i);
});
CompletableFuture.supplyAsync(()->"Hello").thenApply(s->s+" world").thenApply(s->s+"\nThis is CompletableFuture demo").thenApply(String::toLowerCase).whenComplete((result,throwable)-> System.out.println(result));
CompletableFuture future = CompletableFuture.supplyAsync(()->"100").thenApply(s->s+"10").handle((s,t)->s!=null? Double.parseDouble(s):0);
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("completableFuture end!");
CompletableFuture future = CompletableFuture.supplyAsync(()->"100").thenApply(s->s+"10").handle(new BiFunction() {
@Override
public Double apply(String s, Throwable throwable) {
return s!=null?Double.parseDouble(s):0;
}
});
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// 使用lamda表达式的写法:
CompletableFuture future = CompletableFuture.supplyAsync(()->"100").thenApply(s->s+"10").handle((s,t)->s!=null?Double.parseDouble(s):0);
CompletableFuture.supplyAsync(()->"Hello")
.thenApply(s->s+" world")
.thenApply(s->s+"\nThis is CompletableFuture demo")
.thenApply(String::toLowerCase).thenAccept(new Consumer(){
@Override
public void accept(Object o) {
System.out.println(o);
}
});