簡介
這是個接口,擴展了Executor接口,他有四個實現類:
AbstractExecutorService
ForkJoinPool
ScheduledThreadPoolExecutor
ThreadPoolExecutor
API講解
關閉
void shutdown()
List<Runnable> shutdownNow()
這個方法參考這個文章:https://blog.csdn.net/qq_43040688/article/details/106041236
注意一點:在執行完關閉後,線程池就會拒絕任何任務的提交
執行一個方法
<T> Future<T> submit(Callable<T> task)
<T> Future<T> submit(Runnable task,T result)
Future<?> submit(Runnable task)
result
:使用Future的get方法,就會返回這個結果
執行一批方法
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException,
ExecutionException
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException,
ExecutionException,
TimeoutException
- 這是個同步方法
invokeAll
:會返回所有方法的執行結果invokeAny
:會返回任意一個的執行結果;當返回結果後,其他任務不會執行
調試方式
判斷線程池是否終結
boolean isTerminated()
判斷是否執行了終結方法
boolean isShutdown()