线程池类图
|-Java.util.concurrent.Executor 负责线程的使用与调度的根接口
|-ExecutorService:Executor的子接口,线程池的主要接口
|-AbstractExecutorService:实现了ExecutorService接口,基本实现了ExecutorService其中声明的所有方法,另有添加其他方法
|-ThreadPoolExecutor:继承了AbstractExecutorService,主要的常用实现类
|-ScheduledExecutorService:继承了ExecutorService,负责线程调度的接口
|-ScheduledThreadPoolExecutor:继承了ThreadPoolExecutor同时实现了ScheduledExecutorService
Executor接口
public interface Executor { void execute(Runnable command); }
ExecutorService接口
public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); boolean isShutdown(); boolean isTerminated(); boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; Future<?> submit(Runnable task); //实现无返回值的Runnable接口 <T> Future<T> submit(Callable<T> task); //实现有返回值的Callable接口 <T> Future<T> submit(Runnable task, T result); //实现无返回值的Runnable接口并自定义返回值 <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; }
ScheduledExecutorService接口
package java.util.concurrent; public interface ScheduledExecutorService extends ExecutorService { public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit); public <V> ScheduledFuture<V> schedule(Callable<V> callable,long delay, TimeUnit unit); //以固定频率来执行线程任务,固定频率的含义就是可能设定的固定时间不足以完成线程任务,但是它不管,达到设定的延迟时间了就要执行下一次了 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit); //以固定延迟(时间)来执行线程任务,它实际上是不管线程任务的执行时间的,每次都要把任务执行完成后再延迟固定时间后再执行下一次 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit); }