import java.util.concurrent.*;
public class ThreadTest {
private static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws Exception {
// runAsync();
// supplyAsync();
// whenComplete();
// thenApply();
// handle();
// test1();
// test2();
test3();
System.out.println("主线程执行完成。。。。。");
}
public static void runAsync() throws ExecutionException, InterruptedException {
CompletableFuture voidCompletableFuture = CompletableFuture.runAsync(() -> {
try {
Thread.sleep(2000L);
int a = 10 / 2;
System.out.println("结果为:" + a);
} catch (Exception e) {
}
System.out.println("run end ..." + Thread.currentThread().getId());
}, service);
// voidCompletableFuture.get(); 等待完成再返回
}
public static CompletableFuture supplyAsync() throws Exception {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
System.out.println("run end ...");
return System.currentTimeMillis();
},service);
return future;
}
public static void whenComplete() throws ExecutionException, InterruptedException {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("run start ...");
TimeUnit.SECONDS.sleep(1);
Long i = 1000L / 0;
} catch (InterruptedException e) {
}
return 1L;
},service).whenCompleteAsync((res,exception)->{
System.out.println("结果完成"+res);
System.out.println("出现异常:"+exception);
}).exceptionally(t->{
System.out.println(t.getMessage());
return 10L;
});
System.out.println("run end ..." +future.get());
}
private static void thenApply() throws Exception {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("run start ...");
TimeUnit.SECONDS.sleep(1);
Long i = 1000L / 3;
} catch (InterruptedException e) {
}
return 1L;
}, service).thenApply(res -> res * 2).thenApply(res->res * 2);
System.out.println(future.get());
}
private static void handle() throws Exception {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("run start ...");
TimeUnit.SECONDS.sleep(1);
Long i = 1000L / 0;
return 1L;
} catch (InterruptedException e) {
return 2L;
}catch(ArithmeticException e){
return 3L;
}
}, service).handle((t,u)->{
System.out.println(u.getMessage());
return t * 2;
});
System.out.println(future.get());
}
private static void test1() throws Exception {
CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1开始");
System.out.println("任务1结束");
return 1;
}, service);
CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2开始");
System.out.println("任务2结束");
return 2;
}, service);
f1.thenAcceptBothAsync(f2, (a, b) -> {
System.out.println("执行完成t"+a+":"+b);
}, service);
}
public static void test2() throws Exception{
CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1开始");
System.out.println("任务1结束");
return 1;
}, service);
CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2开始");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
System.out.println("任务2结束");
return 2;
}, service);
f1.acceptEitherAsync(f2,(a)->{
System.out.println("执行完成:"+a);
},service);
}
public static void test3() throws Exception{
CompletableFuture f1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1开始");
System.out.println("任务1结束");
return 1;
}, service);
CompletableFuture f2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2开始");
System.out.println("任务2结束");
return 2;
}, service);
CompletableFuture f3 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务3开始");
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
}
System.out.println("任务3结束");
return 3;
}, service);
// CompletableFuture all = CompletableFuture.allOf(f1, f2, f3);
// all.get();
CompletableFuture