public class CompletableFutureDemo02 {
private static ThreadPoolExecutor executor=new ThreadPoolExecutor(5,
50,
10,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
//运行时发现 线程会阻塞 等待新的任务去调度
/**
* 线程串行的方法 thenRunAsync 在前一个线程执行完成之后 开始执行 不会获取前一个线程的返回结果 也不会返结果信息
* @param args thenAcceptAsync 在前一个线程执行完成后 开始执行 获取前一个线程的返回结果 但是不返回结果信息
* thenApplyAsync 在前一个线程执行完成后 开始执行 获取前一个线程的返回结果 同时也会返回结果信息
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("线程开始了" + Thread.currentThread().getName());
int i = 100 / 10;
System.out.println("线程结束了" + Thread.currentThread().getName());
return i;
}, executor).thenApplyAsync(res -> {
System.out.println(res+":"+Thread.currentThread().getName());
return res * 100;
}, executor);
System.out.println("future:"+future.get());
/* CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("线程开始了" + Thread.currentThread().getName());
int i = 100 / 10;
System.out.println("线程结束了" + Thread.currentThread().getName());
return i;
}, executor).thenAcceptAsync(res -> {
System.out.println(res + ":" + Thread.currentThread().getName());
}, executor);*/
/* CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
System.out.println("线程开始了"+Thread.currentThread().getName());
int i = 100 / 10;
System.out.println("线程结束了"+Thread.currentThread().getName());
return i;
}, executor).thenRunAsync(() -> {
System.out.println("线程开始了"+Thread.currentThread().getName());
int i=100/50;
System.out.println("线程结束了"+Thread.currentThread().getName());
}, executor);*/
}
}