1.普通Future的缺点
不知道任务什么时候完成, 获取任务结果时必须要get,如果get不到就会同步阻塞, 没有函数回调
问:什么是同步,什么是异步
需要等待结果返回,才能运行是同步
不需要等待结果返回,就能运行是异步
函数回调: 任务完成后会通知调用它的线程
public class FutureDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(5);
//读取敏感词汇
Future<String[]> filterWordsFuture = executor.submit(() -> {
String str = CommonUtils.readFile("filter_words.txt");
return str.split(",");
});
//读新闻稿
Future<String> newsFuture = executor.submit(() -> {
return CommonUtils.readFile("news.txt");
});
//替换操作
Future<String> replaceFuture = executor.submit(() -> {
String[] words = filterWordsFuture.get();
String news = newsFuture.get();
for (String word : words) {
news = news.replaceAll(word, "*");
}
return news;
});
//打印替换后的结果
String news = replaceFuture.get();
System.out.println(news);
}
}
2.CompletableFuture的优势
- 提供各种场景的回调函数
- 可以组合多个Future
- 提供全方面的异常处理
- 无缝衔接lambda表达式和Stream API
3.创建异步任务
1.runAsync
开启一个无返回值的异步任务
public class RunAsyncDemo {
public static void main(String[] args) {
CommonUtils.printTheadLog("主线程 start");
CompletableFuture.runAsync(() -> {
CommonUtils.printTheadLog("读取文件,异步任务开始");
CommonUtils.sleepSecond(3);
CommonUtils.printTheadLog("读取文件,异步任务结束");
});
CommonUtils.printTheadLog("主线程继续");
CommonUtils.sleepSecond(4);
CommonUtils.printTheadLog("主线程结束");
}
}
2.supplyAsync
开启一个有返回值的异步任务
public class SupplyAsyncDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CommonUtils.printTheadLog("主线程 start");
CompletableFuture<String> future = CompletableFuture
.supplyAsync(() -> CommonUtils.readFile("news.txt"));
CommonUtils.printTheadLog("主线程继续");
System.out.println("news :" +future.get() );
CommonUtils.printTheadLog("主线程 end");
}
}
3.回调函数
1.thenApply
接收上一个异步任务的返回值, 并自己也有返回值的回调
2.thenAccept
接收上一个异步任务的返回值, 但自己无返回值的回调
3.thenRun
不接受上一个异步任务的返回值,也没有返回值
4.编排异步任务
1.thenCompose 两个依赖的异步任务
2.thencombine 两个不依赖的异步任务
3.编排多个异步任务
1.allof 无返回值
get会抛异常 join不会