5.CompletableFuture
jdk 1.8 新增 可以 串行化执行线程的 工具类
一个completetableFuture 对象 就代表了一个任务
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class T7ThreadPoolTest {
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main --- start...");
// runAsync() 没有返回结果
// CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getName());
// int i = 10/2;
// System.out.println("结束线程:" + Thread.currentThread().getName() + "\t, 运行结果:" + i);
// }, executorService);
CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getName());
int i = 10/2;
System.out.println("结束线程:" + Thread.currentThread().getName() + "\t, 运行结果:" + i);
return i;
}, executorService);
Integer integer = uCompletableFuture.get();
System.out.println("main --- end..., 运行结果:" + integer);
}
}
串行化操作
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class T8ThreadPoolTest {
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main --- start...");
// // runAsync() 没有返回结果, 后面的任务也没有返回值
// CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getName());
// int i = 10/2;
// System.out.println("结束线程:" + Thread.currentThread().getName() + "\t, 运行结果:" + i);
// }, executorService).thenRunAsync(()->{
// System.out.println("执行任务2..." + Thread.currentThread().getName());
// },executorService);
// 将 第一个线程的返回值 传递到第二个线程里
// CompletableFuture.supplyAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getName());
// int i = 10/2;
// System.out.println("结束线程:" + Thread.currentThread().getName() + "\t, 运行结果:" + i);
// return i;
// }, executorService).thenAcceptAsync((resp)->{
// // 可以接 前面线程执行完成的 返回值, 但 此线程不返回
// System.out.println("执行任务2..." + Thread.currentThread().getName() + ", 接收到参数:" + resp);
// },executorService);
// 将 第一个线程的返回值 传递到第二个线程里, 第二个线程也有返回值
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getName());
int i = 10 / 2;
System.out.println("结束线程:" + Thread.currentThread().getName() + "\t, 运行结果:" + i);
return i;
}, executorService).thenApplyAsync((resp) -> {
// 可以接 前面线程执行完成的 返回值
System.out.println("执行任务2..." + Thread.currentThread().getName() + ", 接收到参数:" + resp);
return resp;
}, executorService);
Integer integer = integerCompletableFuture.get();
System.out.println("main --- end..., 运行结果:" + integer);
}
}