线程池
架构:
executor 接口 executors 类
ExecutorService 接口
AbstractExecutorService 抽象类 ScheduledExecutorService 接口
ThreadPoolExecutor 类 ScheduledThreadPoolExecutor 类
Executor 接口
将任务的提交与任务的执行分离
void execute(Runnable command)
ExecutorService 接口(继承 Executor 接口)
为 executor 接口服务
submit
invoke
shutdown
AbstractExecutorService 抽象类(实现 ExecutorService 接口)
继承 ExecutorService 接口,实现该接口的方法
ThreadPoolExecutor 类(继承 ThreadPoolExecutor 类)
//-------------------------------------------------------------------- private final BlockingQueue\<Runnable> workQueue; // 阻塞队列 private final ReentrantLock mainLock = new ReentrantLock(); // 互斥锁,锁住的是线程池 private final HashSet workers = new HashSet(); // 线程集合 private final Condition termination = mainLock.newCondition(); // “终止条件”,与“mainLock”绑定 //-------------------------------------------------------------------- private volatile int corePoolSize; private volatile int maximumPoolSize; private volatile boolean allowCoreThreadTimeOut; // 是否允许线程在空闲状态时,仍然能够存活 private volatile long keepAliveTime; private volatile ThreadFactory threadFactory; private volatile RejectedExecutionHandler handler; // handler是RejectedExecutionHandler类型。它是"线程池拒绝策略"的句柄,也就是说"当某任务添加到线程池中,而线程池拒绝该任务时,线程池会通过handler进行相应的处理 // ThreadPoolExecutor.DiscardPolicy:无法执行的任务将被删除。 // ThreadPoolExecutor.AbortPolicy:默认策略,任务遭到拒绝,直接抛出异常RejectedExecutionException。 // ThreadPoolExecutor.CallerRunsPolicy:让调用者所在的线程来运行该任务。 // ThreadPoolExecutor.DiscardOldestPolicy:将位于阻塞队列头部的任务删除,然后尝试重新执行任务(失败的话重复此过程)。 //-------------------------------------------------------------------- private int largestPoolSize; // 线程池中线程数量曾经达到过的最大值 private long completedTaskCount; // 已完成任务数量 //-------------------------------------------------------------------- // 线程池的生命周期 RUNNING->SHUTDOWN(调用shutdown()方法后)、STOP(调用shutdwonNow()方法后->TIGYING(所有任务已终止)->TERMINATED(调用terminated()方法后) //-------------------------------------------------------------------- // 构造器 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue\<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { // threadFactory来自Executors.defaultThreadFactory() } //-------------------------------------------------------------------- // 添加任务到线程池 public void execute(Runnable command) { // 情况1: // 情况2: // 情况3: } // 添加任务到线程池,实际上还是通过调用execute方法 public Future submit(Runnable task) { } // 添加任务到线程池,并创建一个线程启动该任务 // core为true的话,则以corePoolSize为界限,若"线程池中已有任务数量>=corePoolSize",则返回false;core为false的话,则以maximumPoolSize为界限,若"线程池中已有任务数量>=maximumPoolSize",则返回false。 private boolean addWorker(Runnable firstTask, boolean core) { } //-------------------------------------------------------------------- // 关闭线程池 public void shutdown() { } public void shutdownNow() { }
ScheduledExecutorService 接口(继承 Executor 接口)
相当于提供了"延时"和"周期执行"功能的 ExecutorService
ScheduledThreadPoolExecutor 类(实现 ScheduledExecutorService 接口)
ScheduledThreadPoolExecutor
Executors 类
是个静态工厂类。它通过静态工厂方法返回 ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 等类的对象。
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue\<Runnable>()); } public static ThreadFactory defaultThreadFactory() { return new DefaultThreadFactory(); }
当我们需要获取线程的执行结果时,就需要用到 Callable 和 Future。Callable 用于产生结果(将 Callable 的实现类的对象交给线程池去执行),Future 用于获取结果。
Callable 接口:
V call();
Future 接口:
V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; ....
RunnableFuture(继承了 Future 接口和 Runnable 接口)
public interface RunnableFuture\<V> extends Runnable, Future\<V> { }
FutureTask 类(简介实现 RunnableFuture 接口)
参考:
java.util.concurrrent 包源码
http://www.cnblogs.com/skywang12345/p/java_threads_category.html
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于