创建异步对象
CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
System.out.println("当前线程:"+ Thread.currentThread().getId());
int i = 10/2;
System.out.println("任务1运行结果:"+ i);
}
}, executor);
Integer integer = future.get();
计算完成时回调方法
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor).whenComplete(new BiConsumer<Integer, Throwable>() {
@Override
public void accept(Integer integer, Throwable throwable) {
System.out.println("异步任务成功完成了。。。结果是。"+integer+"异常是:"+throwable);
}
}).exceptionally(new Function<Throwable, Integer>() {
@Override
public Integer apply(Throwable throwable) {
return 10;
}
});
Integer integer = future.get();
handler方法
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor).handle(new BiFunction<Integer, Throwable, Integer>() {
@Override
public Integer apply(Integer result, Throwable throwable) {
if(result!=null) {
return result*2;
}
if(throwable!=null){
return 0;
}
return 0;
}
});
Integer integer = future.get();
线程串行化
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor).thenRunAsync(new Runnable() {
@Override
public void run() {
System.out.println("任务2启动了");
}
});
CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor).thenAcceptAsync(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println("任务2启动了。。。"+integer);
}
});
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor).thenApplyAsync(new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
System.out.println("任务2启动了");
return "任务2启动的返回,并携带上一步信息"+integer;
}
}, executor);
System.out.println(future.get());
两个线程都完成
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束并返回'hello'");
return "hello";
}
}, executor);
future01.runAfterBothAsync(future02, new Runnable() {
@Override
public void run() {
System.out.println("任务3线程开始。。。");
}
},executor);
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束并返回'hello'");
return "hello";
}
}, executor);
future01.thenAcceptBothAsync(future02, new BiConsumer<Integer, String>() {
@Override
public void accept(Integer integer, String s) {
System.out.println("任务3开始。。。之前接过:"+integer+"-->>>"+s);
}
},executor);
System.out.println("main---start---");
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束并返回'hello'");
return "hello";
}
}, executor);
CompletableFuture<Object> future = future01.thenCombineAsync(future02, new BiFunction<Integer, String, Object>() {
@Override
public Object apply(Integer integer, String s) {
return integer + ":" + s + "-->>haha";
}
}, executor);
System.out.println("main...end..."+future.get());
完成任意一个线程返回结果
System.out.println("main---start---");
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}
}, executor);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
System.out.println("任务2结束并返回'hello'");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}
}, executor);
future01.runAfterEither(future02, new Runnable() {
@Override
public void run() {
System.out.println("任务3开始。。。执行结果");
}
});
System.out.println("main...end...");
System.out.println("main---start---");
CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
int i = 0;
try {
System.out.println("当前线程:" + Thread.currentThread().getId());
Thread.sleep(2000);
i = 10 / 2;
System.out.println("任务1运行结果:" + i);
} catch (Exception e) {
e.printStackTrace();
}
return i;
}, executor);
CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束并返回'hello'");
return "hello";
}, executor);
future01.acceptEitherAsync(future02, result -> {
System.out.println("感知到的返回信息"+result);
},executor);
System.out.println("main...end...");
多任务组合
System.out.println("main---start---");
CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品图片信息");
return "hello.jpg";
}, executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品属性信息");
return "黑色+256G";
}, executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品详情信息");
return "华为P40";
}, executor);
CompletableFuture.allOf(futureImg,futureAttr,futureDesc);
System.out.println("main...end..."+futureImg.get()+"->"+futureAttr.get()+"->"+futureDesc.get());
System.out.println("main---start---");
CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品图片信息");
return "hello.jpg";
}, executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品属性信息");
return "黑色+256G";
}, executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品详情信息");
return "华为P40";
}, executor);
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
System.out.println("main...end..."+anyOf.get());