ExecutorService是java.util.concurrent包下的一个接口,作为一个线程池接口,主要负责任务执行管理。
Java API对ExecutorService接口的具体实现类主要有三个,如下所示:
ThreadPoolExecutorForkJoinPoolScheduledThreadPoolExecutor
主要UML图如下所示
我们创建ExecutorService时一般是直接通过java提供的工具类Executors工厂类进行创建。而Executors工厂类一共提供了下列方法区创建线程池。
1. newFixedThreadPool 2. newWorkStealingPool 3. newSingleThreadExecutor 4. newCachedThreadPool 5. newSingleThreadScheduledExecutor 6. newScheduledThreadPool
newFixedThreadPool
创建一个线程数量固定的线程池newWorkStealingPool
返回一个使用所有可用处理器作为目标并行度的ForkJoinPool线程池newSingleThreadExecutor
创建一个单线程线程池newCachedThreadPool
创建一个可缓存的线程池,需要时创建线程,不需要则销毁newSingleThreadScheduledExecutor
创建一个单线程定时执行器(ScheduledExecutorService)newScheduledThreadPool
创建一个定时执行器
ExecutorService的使用
ExecutorService提供任务时可以提交Runnable与Callable来执行任务并返回Future用于接受Callable中返回的数据,
submit(Callable callable)提交Callable
ExecutorService executorService = Executors.newSingleThreadExecutor(); Futuresubmit(Runnable run)submit = executorService.submit(() -> { TimeUtils.sleepMilliSecond(1000);// 自定义工具类 return "abs"; }); submit.get(100, TimeUnit.MILLISECONDS);// 获取Callable返回的对象,如果在指定时间内没有返回成功则抛出异常. submit.get();// 获取Callable返回的对象,直到Callable成功返回,否则堵塞.
提交Runnable
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
LogUtils.info("执行Runnable");
});
invokeAny
执行Callable列表,当存在一个线程执行完毕并返回结果时,invokeAny执行结束并返回结果.
ExecutorService executorService = Executors.newCachedThreadPool(); ListinvokeAll> callables = new ArrayList<>(); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "a"; }); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "b"; }); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "c"; }); String s = executorService.invokeAny(callables); LogUtils.info("s = {}", s);
执行Callable列表,并在任务执行完毕后返回future列表用于获取信息
ExecutorService executorService = Executors.newCachedThreadPool(); Listshutdown> callables = new ArrayList<>(); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "a"; }); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "b"; }); callables.add(() -> { TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000)); return "c"; }); List > futures = executorService.invokeAll(callables); for (Future future : futures) { String s = future.get(); LogUtils.info("s = {}", s); }
等待线程池中所有任务结束然后停止
shutdownNow立即停止线程池
isShutdown判定线程池是否被停止
isTerminated判定线程池任务是否结束
awaitTermination当调用了awaitTermination后有一下一种状态.
1. 没有超时,并且子线程执行完,此时返回true 2. 没有超时,但子线程没有执行完,此时返回false 3. 超时,此时返回false
在执行awaitTermination前需要执行shutdown方法,否则awaitTermination一定超时并且返回false
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
LogUtils.info(Thread.currentThread().getName());
TimeUtils.sleepMilliSecond(RandomUtils.nextInt(0, 1000));
});
executorService.shutdown();
LogUtils.info("开始等待");
boolean b = executorService.awaitTermination(3, TimeUnit.SECONDS);
LogUtils.info("等待完毕");
if(b){
LogUtils.info("分线程已经结束");
}
LogUtils.info(Thread.currentThread().getName());



