1.之前实现线程有三种方式
继承Thread类,实现Runnable或者Callable接口。
如果实现Runnable或者Callable接口,需要Thread帮我们启动,但是现在提供了Executor帮助我们管理并执行实现了Runnable和Callable的类。
2.Executor的继承关系
其中Executor接口是提供了一种方法execute(Runnable run)用来执行定义的任务;
public interface Executor { /** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the <tt>Executor</tt> implementation. */ void execute(Runnable command); }
接口ExecutorServices实现了Executor接口,但是没有实现executue()方法;并提供了submit()方法执行任务;
public interface ExecutorService extends Executor { /** * Submits a Runnable task for execution and returns a Future * representing that task. The Future's <tt>get</tt> method will * return the given result upon successful completion. *
使用submit()方法提交要执行的任务,
返回Future对象,Future保存的是其他线程异步执行的结果;
如果不需要返回结果的话,可以调用ThreadPoolExecutor实现的execute()方法执行任务; */
<T> Future<T> submit(Callable<T> task);
}
ThreadPoolExecutor实现了Executor接口
public class ThreadPoolExecutor extends AbstractExecutorService { /** ThreadPoolExecutor的构造函数,用来创建一个线程池 corePoolSize:线程池的大小 maxinumPoolSize:线程池中允许同时执行的最多线程数;
keepAliveTime:如果线程池中线程的数量>corePoolSize,且超过的部分线程空闲时间>keepAliveTime会被销毁;
unit:keepAliveTime的单位;
workQueue:在任务被执行之前持有任务直到任务被执行;
如果线程池中的线程数量<corePoolSize,有新任务到来新建线程执行;如果线程数量>corePoolSize,将新任务放入队列中,等待执行;如果队列已满且线程数量<maxinumPoolSize,新建
线程执行任务; */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; } /** ThreadPoolExecutor了execute方法,具体实现细节以后再说 **/ public void execute(Runnable command) { if (command == null) throw new NullPointerException(); int c = ctl.get(); if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); } if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); else if (workerCountOf(recheck) == 0) addWorker(null, false); } else if (!addWorker(command, false)) reject(command); } }
除了上面的几个类或接口之外,还有一个比较重要的类 Executors;该类是一个工厂类,能够产出几种不同的线程池;
public class Executors { /* * 利用ThreadPoolExecutor生成固定的线程池 */ public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } /** *这个线程池可以重用重用线程,当线程池没有可用的线程时,新建线程;如果一个线程超过60s没有被使用,那该线程将从线程池中移除; **/ public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } }