互联网网站建设水平,昆明网签备案查询,深圳企业网站制作,成都做网站设计公司价格文章目录 前言1. Future 线程池2. 什么是CompletableFuture 前言
我们异步执行一个任务时#xff0c;需要用线程池Executor去创建#xff0c;有两种方式#xff1a;
如果不需要有返回值#xff0c; 任务继承Thread类或实现Runnable接口#xff1b;如果需要有返回值 线程池2. 什么是CompletableFuture 前言
我们异步执行一个任务时需要用线程池Executor去创建有两种方式
如果不需要有返回值 任务继承Thread类或实现Runnable接口如果需要有返回值任务实现Callable接口调用Executor的submit方法执行任务再使用Future.get()获取任务结果。
当我们得到包含结果的Future时我们可以使用get方法等待线程完成并获取返回值但问题是Future的get()方法会阻塞主线程。并且当多个线程存在依赖组合的时候使用同步组件CountDownLatch是比较麻烦的。
Java8新增的CompletableFuture类提供了非常强大的Future的扩展功能提供了函数式编程的能力可以通过回调的方式处理计算结果并且提供了组合多个异步任务的方法。并且CompletableFuture是非阻塞性的就是在主线程之外创建一个独立的线程用以运行一个非阻塞的任务然后将结果通知主线程。通过这种方式主线程不用为了任务的完成而阻塞极大提升了程序的性能。
1. Future 线程池
Future是Java5引入的新接口提供了异步并行计算的能力。如果主线程需要执行一个耗时的计算任务我们就可以通过Future把这个任务放到异步线程中执行主线程继续处理其他任务处理完成后再通过Future获取计算结果。
举个例子假设我们有两个查询任务一个查询运动员信息一个查询运动员的荣誉信息。
public class PlayerInfo {private String name;private int age;}public class PlayerInfoService {public PlayerInfo getPlayerInfo() throws InterruptedException {Thread.sleep(300);//模拟调用耗时return new PlayerInfo(Kobe, 32);}}public class MedalInfo {private String medalCategory;private int medalNum;}public class MedalInfoService {public MedalInfo getMedalInfo() throws InterruptedException {Thread.sleep(500); //模拟调用耗时return new MedalInfo(MVP, 1);}}使用Future线程池的方式实现异步任务
public class FutureTest {public static void main(String[] args) throws InterruptedException, ExecutionException {// 1. 创建线程池ExecutorService executorService Executors.newFixedThreadPool(10);// 2. 任务加入线程池PlayerInfoService playerInfoService new PlayerInfoService();MedalInfoService medalInfoService new MedalInfoService();long startTime System.currentTimeMillis();FutureTaskPlayerInfo playerInfoFutureTask new FutureTask(new CallablePlayerInfo() {Overridepublic PlayerInfo call() throws Exception {return playerInfoService.getPlayerInfo();}});executorService.submit(playerInfoFutureTask);Thread.sleep(400);FutureTaskMedalInfo medalInfoFutureTask new FutureTask(new CallableMedalInfo(){Overridepublic MedalInfo call() throws Exception {return medalInfoService.getMedalInfo();}});executorService.submit(medalInfoFutureTask);PlayerInfo playerInfo playerInfoFutureTask.get();MedalInfo medalInfo medalInfoFutureTask.get();System.out.println(总共用时 (System.currentTimeMillis() - startTime) ms);// 3. 关闭线程池executorService.shutdown();}}运行结果
总共用时905ms如果是在主线程串行执行的话耗时大约为3005004001200ms。可见future线程池的异步方式提高了程序的执行效率。但是Future对于结果的获取只能通过阻塞或者轮询的方式得到任务的结果。
Future.get()就是阻塞调用在线程获取结果之前get方法会一直阻塞。阻塞的方式和异步编程的设计理念相违背。Future提供了一个isDone方法可以在程序中轮询这个方法查询执行结果。轮询的方式会耗费CPU资源。 轮询——在计算机网络中轮询Polling是一种通信方式其中一个节点通常是客户端定期发送请求到另一个节点通常是服务器以获取数据或状态。这种方式的缺点在于即使没有可用的数据客户端也会持续不断地发送请求这会消耗大量的CPU资源。此外如果轮询的间隔设置得太短可能会导致网络拥塞甚至可能影响实时性。因此轮询并不是一种高效的通信方式特别是在需要高性能和低延迟的应用中。 2. 什么是CompletableFuture