Executor线程池解析
来源:互联网 发布:中西方音乐文化知乎 编辑:程序博客网 时间:2024/05/22 03:03
1 概述
Executor
框架是一个根据一组执行策略调用,调度,执行和控制的异步任务的框架。其位于java.util.concurrent
包中。它提供了一种将”任务提交”与”任务运行”分离开来的机制。
线程池有两个作用:
1、避免thread不断创建销毁的开销;2、通过使用线程池可以限制这些任务所消耗的资源,比如最大线程数,最大的消息缓冲池等,已达到最佳的运行效果。
其包含了一系列的接口,和实现类,工具类等,实现了一套线程池机制,线程池包含一个任务队列和多个线程。首先添加任务进任务队列,然后在启动规定个数的线程执行队列中的任务,剩下的任务则在队列中排队等待。线程池有空闲线程的时候会从队列头部取一个新的任务执行。
2 Executor
Java里面线程池的顶级接口是Executor,定义了一个接口,表示可以执行Runnable方法。
public interface Executor { void execute(Runnable command);}
3 ExecutorService
ExecutorService接口扩展了Executor接口,增加了很多线程池相关方法,即一些生命周期管理的方法。ExecutorService的生命周期有三种状态:运行,关闭,终止。ExecutorService创建时处于运行状态。当调用ExecutorService.shutdown()后,处于关闭状态,isShutdown()方法返回true。这时,不应该再想Executor中添加任务,所有已添加的任务执行完毕后,Executor处于终止状态,isTerminated()返回true。
其中shutdown和shutdownNow的区别是,前者拒绝添加新任务进来,但是会执行完当前正在执行的和队列中的线程,后者是直接结束所有正在运行的线程,删除等待队列中的线程。
public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); boolean isShutdown(); boolean isTerminated(); //阻塞在这个方法,等待所有子线程执行结束后继续运行后面的代码。 boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; <T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); Future<?> submit(Runnable task);}
ExecutoreService提供了submit()方法,传递一个Callable,或Runnable,返回Future。如果ExecutoreService后台线程池还没有完成Callable的运行,这调用返回Future对象的get()方法,会阻塞直到运行完成。
4、ThreadPoolExecutor
ExecutorService接口的默认实现类,继承了AbstractExecutorService。
4.1 构造方法如下:
private final BlockingQueue<Runnable> workQueue; // 阻塞队列 private final ReentrantLock mainLock = new ReentrantLock(); // 互斥锁 private final HashSet<Worker> workers = new HashSet<Worker>();// 线程集合.一个Worker对应一个线程 private final Condition termination = mainLock.newCondition();// 终止条件 private int largestPoolSize; // 线程池中线程数量曾经达到过的最大值。 private long completedTaskCount; // 已完成任务数量 private volatile ThreadFactory threadFactory; // ThreadFactory对象,用于创建线程。 private volatile RejectedExecutionHandler handler;// 拒绝策略的处理句柄 private volatile long keepAliveTime; // 线程池维护的线程所允许的空闲时间,超过这个时间的线程若无任务处理,将会被销毁。 private volatile boolean allowCoreThreadTimeOut; private volatile int corePoolSize; // 线程池维护线程的最小数量,哪怕是空闲的。 private volatile int maximumPoolSize; // 线程池维护的最大线程数量 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);}public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler);}public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler);}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;}
4.2参数分析:
corePoolSize与maximumPoolSize 前者是最少线程数,后者是最大线程数。
若执行execute方法添加新任务:
1.若当前线程数小于corePoolSize的时候,则直接创建新线程执行;
2.若当前线程数等于corePoolSize,且workQueue未满,则请求添加到workQueue中排队等待。
3.若当前线程数等于corePoolSize小于maximumPoolSize,且workQueue已满,则创建新线程处理任务。
4.若当前线程数等于maximumPoolSize,且workQueue已满,则通过handler所指定的策略来处理新请求。具体策略,参考后面RejectedExecutionHandler
5.若将maximumPoolSize 设置为基本的无界值(如 Integer.MAX_VALUE),则允许池适应任意数量的并发任务。
6.当线程数大于corePoolSize的时候,多余的线程会等待keepAliveTime长的时间,如果无请求可处理就自行销毁。
BlockingQueue BlockingQueue是一个接口,规定了当队列为空或者已满的时候,需要阻塞以等待生产者/消费者协同操作并唤醒线程。该缓冲队列的长度决定了能够缓冲的最大数量。
public interface BlockingQueue<E> extends Queue<E> { boolean add(E e); boolean offer(E e); void put(E e) throws InterruptedException; boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException; E take() throws InterruptedException; E poll(long timeout, TimeUnit unit) throws InterruptedException; int remainingCapacity(); boolean remove(Object o); public boolean contains(Object o); int drainTo(Collection<? super E> c); int drainTo(Collection<? super E> c, int maxElements);}
缓冲队列有三种通用策略:
1、 直接提交: 例如SynchronousQueue,此队列是将任务直接提交给线程,而不保持它们。若不存在可用于立即运行的线程,则创建一个新线程。此策略可避免在处理可能具有内部依赖性的请求集时出现锁。
2、无界队列:例如,不具有预定义容量的LinkedBlockingQueue,此此队列将导致在所有corePoolSize 线程都忙时新任务在队列中等待。maximumPoolSize失去意义,因为最大线程数就是corePoolSize。
3、有界队列:如 ArrayBlockingQueue,有助于防止资源耗尽
ThreadFactory 创建线程的工厂。
public interface ThreadFactory { Thread newThread(Runnable r); }
1.默认使用Executors.defaultThreadFactory() 创建线程,设置Daemon为false,设置优先级为Thread.NORM_PRIORITY。
public static ThreadFactory defaultThreadFactory() { return new DefaultThreadFactory();}static class DefaultThreadFactory implements ThreadFactory { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final ThreadGroup group; private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix; DefaultThreadFactory() { SecurityManager s = System.getSecurityManager(); group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; } public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.isDaemon()) t.setDaemon(false); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; }}
2.通过提供不同的 ThreadFactory,可以改变线程的名称、线程组、优先级、守护进程状态等等
3.若从newThread返回null,则ThreadFactory未能创建线程,但执行程序将继续运行,不过不能执行任何任务。
RejectedExecutionHandler: 当Executor已经关闭(即执行了executorService.shutdown()方法后),并且满足corePoolSize与maximumPoolSize中第4时,新任务被拒绝的策略。
public interface RejectedExecutionHandler { void rejectedExecution(Runnable r, ThreadPoolExecutor executor);}
目前有以下4种预定义策略:
1、ThreadPoolExecutor.AbortPolicy 处理程序遭到拒绝将抛出运行时 RejectedExecutionException,默认使用的是此策略。
public static class AbortPolicy implements RejectedExecutionHandler { public AbortPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); }}
2、ThreadPoolExecutor.CallerRunsPolicy 线程调用运行该任务的execute本身。此策略提供简单的反馈控制机制,能够减缓新任务的提交速度
public static class CallerRunsPolicy implements RejectedExecutionHandler { public CallerRunsPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } }}
3、ThreadPoolExecutor.DiscardPolicy 不能执行的任务将被删除;
public static class DiscardPolicy implements RejectedExecutionHandler { public DiscardPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { }}
4、ThreadPoolExecutor.DiscardOldestPolicy 如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)
public static class DiscardOldestPolicy implements RejectedExecutionHandler { public DiscardOldestPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } }}
4.3 execute
execute方法是添加新任务的时候调用的方法,内部执行了addWorker方法,来添加一个任务或者执行了reject方法来调用拒绝策略。
public void execute(Runnable command) { if (command == null) throw new NullPointerException(); //这里获取了当前线程大小状态的存储信息,在通过workerCountOf获取运行中线程数量。 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);}
4.4 addWorker方法
经过一系列的判断后,new了一个Worker,然后添加进队列然后执行起来。
private boolean addWorker(Runnable firstTask, boolean core) { retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && ! (rs == SHUTDOWN && firstTask == null && ! workQueue.isEmpty())) return false; for (;;) { int wc = workerCountOf(c); if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize)) return false; if (compareAndIncrementWorkerCount(c)) break retry; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop } } boolean workerStarted = false; boolean workerAdded = false; Worker w = null; try { w = new Worker(firstTask); final Thread t = w.thread; if (t != null) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // Recheck while holding lock. // Back out on ThreadFactory failure or if // shut down before lock acquired. int rs = runStateOf(ctl.get()); if (rs < SHUTDOWN || (rs == SHUTDOWN && firstTask == null)) { if (t.isAlive()) // precheck that t is startable throw new IllegalThreadStateException(); workers.add(w); int s = workers.size(); if (s > largestPoolSize) largestPoolSize = s; workerAdded = true; } } finally { mainLock.unlock(); } if (workerAdded) { t.start(); workerStarted = true; } } } finally { if (! workerStarted) addWorkerFailed(w); } return workerStarted;}
4.5 worker线程
worker自己实现了Runnable,它自己维护了执行它的线程对象thead,又维护了一个Runnable对象firstTask(任务对象)
private final class Worker extends AbstractQueuedSynchronizer implements Runnable{ private static final long serialVersionUID = 6138294804551838833L; final Thread thread; Runnable firstTask; volatile long completedTasks; Worker(Runnable firstTask) { setState(-1); // inhibit interrupts until runWorker this.firstTask = firstTask; this.thread = getThreadFactory().newThread(this); } /** Delegates main run loop to outer runWorker. */ public void run() { runWorker(this); } // Lock methods // // The value 0 represents the unlocked state. // The value 1 represents the locked state. protected boolean isHeldExclusively() { return getState() != 0; } protected boolean tryAcquire(int unused) { if (compareAndSetState(0, 1)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; } protected boolean tryRelease(int unused) { setExclusiveOwnerThread(null); setState(0); return true; } public void lock() { acquire(1); } public boolean tryLock() { return tryAcquire(1); } public void unlock() { release(1); } public boolean isLocked() { return isHeldExclusively(); } void interruptIfStarted() { Thread t; if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) { try { t.interrupt(); } catch (SecurityException ignore) { } } }}
4.6 runWorker方法
用于执行工作线程,其中worker也是一个封装类,实现了Runnable接口。
final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts boolean completedAbruptly = true; try { while (task != null || (task = getTask()) != null) { w.lock(); // If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted. This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); }}
4 ScheduledExecutorService
此service接口是为了支持时间可控的任务执行而设计,其中包括:固定延迟执行,周期性执行等,和Timer/TimerTask类似。
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);}
6、ScheduledThreadPoolExecutor
ScheduledExecutorService接口的默认实现类,并且继承了ThreadPoolExecutor。
7、Executors
Executor线程池的工厂方法,提供了一套创建各种线程池的机制。包括如下
固定大小线程池newFixedThreadPool,也就是无界的线程池,因为LinkedBlockingQueue是无界的
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);}
单线程线程池newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));}public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory));}
无界线程池,可以进行自动线程回收newCachedThreadPool
其中用到了SynchronousQueue:每个插入操作必须等待另一个线程的对应移除操作完成。(就是缓冲区为1的生产者消费者模式)public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());}public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);}
newSingleThreadScheduledExecutor 创建一个单线程执行程序,它可以定期执行命令。
public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1));} public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1, threadFactory));}
- newScheduledThreadPool 定长线程池,支持定时的周期执行任务,相当于timer。
与timer区别是:timer只能创建唯一的线程来执行所有timer任务。如果一个TimerTask耗时多,会影响其他TimerTask的时效准确性。Timer对异常支持不好。
其中scheduleAtFixedRate方法可以指定执行的周期等参数
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize);} public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);}
8、CompletionService 完成服务
public interface CompletionService<V> { Future<V> submit(Callable<V> task); Future<V> submit(Runnable task, V result); Future<V> take() throws InterruptedException; Future<V> poll(); Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;}
其中poll方法不会等待,而是返回null,而take方法会等待。
9、ExecutorCompletionService
CompletionService方法的实现类。在构造方法中提供了一个LinkedBlockingQueue。
public ExecutorCompletionService(Executor executor) { if (executor == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; this.completionQueue = new LinkedBlockingQueue<Future<V>>();}public ExecutorCompletionService(Executor executor, BlockingQueue<Future<V>> completionQueue) { if (executor == null || completionQueue == null) throw new NullPointerException(); this.executor = executor; this.aes = (executor instanceof AbstractExecutorService) ? (AbstractExecutorService) executor : null; this.completionQueue = completionQueue;}
10、Future
public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning);// 试图取消对此任务的执行 boolean isCancelled(); // 如果在任务正常完成前将其取消,则返回 true boolean isDone(); // 如果任务已完成,则返回 true // 如有必要,等待计算完成,然后获取其结果 V get() throws InterruptedException, ExecutionException; // 如有必要,最多等待为使计算完成所给定的时间之后,获取其结果(如果结果可用)。 V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;}
Future接口表示异步任务,是还没有完成的任务给出的未来结果。可以用来判断任务是否完成,用来中断任务,以及获取任务的执行结果。
get()方法: 等待Callable结束并获取它的执行结果,没有超时参数的get方法会一直阻塞在这里等待,直到任务结束,有timeout参数的,在等待timeout后,如果还没有成功,则返回null,且抛出TimeOutException,而不是继续执行后面的代码。cancel(boolean mayInterruptIfRunning)方法: 试图取消任务,若取消成功返回true,取消失败返回false(取消已完成的任务也会返回false)。参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务.若任务正在执行,且参数mayInterruptIfRunning为true,则必定返回false。isCancelled()方法:表示任务是否被取消成功,若任务正常完成前被取消成功,则返回true,否则falseisDone(): 比奥斯任务是否已经完成,已经完成返回true
11 FutureTask
FutureTask实现了RunnableFuture接口,RunnableFuture接口继承了Future和Runnable接口。故FutureTask既可以当做线程执行,也可以当做Future得到Callable的返回值。如果不想分支线程阻塞主线程,又想取得分支线程的执行结果,就用FutureTask
public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); this.callable = callable; this.state = NEW; // ensure visibility of callable}public FutureTask(Runnable runnable, V result) { this.callable = Executors.callable(runnable, result); this.state = NEW; // ensure visibility of callable}
12、Callable
public interface Callable<V> { V call() throws Exception;}
Callable也是一种任务,类似Runnable,区别在于:
Runnable没有返回值,无法抛出经过检查的异常;Callable有返回值而且当获取返回结果时可能会抛出异常。
Callable 的 call()方法只能通过 ExecutorService 的 submit(Callable task) 方法来执行,并且返回一个表示任务等待完成的Future,如果 Future 的返回尚未完成,则 Future.get()方法会阻塞等待,直到 Future 完成返回,也可以通过调用 isDone()方法判断 Future 是否完成了返回。但是Runnable通过 ExecutorService 的 submit(Callable task) 方法来执行,返回的Future的get方法返回的是null。
13、示例
try { Future<String> future1 = es.submit(task1); System.out.println("task1 " + future1.get()); System.out.println("task1 isDone " + future1.isDone()); future2 = es.submit(task2); System.out.println("task2 " + future2.get(5, TimeUnit.SECONDS));}catch(TimeoutException e){ System.out.println("+++task2 cancel " + future2.cancel(true)); System.out.println("+++task2 isCanceled "+ future2.isCancelled()); Future<String> future3 = es.submit(task3); try { System.out.println("+++task3 " + future3.get()); } catch (InterruptedException | ExecutionException e1) { e1.printStackTrace(); }}
示例源码见github
- Executor线程池解析
- Executor线程,线程池
- Java Executor并发框架(十)Executor框架线程池源码解析
- Executor线程池实例
- 线程池与Executor
- Executor线程池
- Executor线程池扩展
- java线程池Executor
- Executor和线程池
- Java 线程池(Executor)
- Java 线程池-Executor
- 线程池Executor与ExecutorService
- Java 线程池(一)---Executor
- Executor线程池提交任务
- Executor框架的线程池
- Executor框架与线程池
- Executor框架和线程池
- 《Java线程池》:Executor生命周期
- Shell脚本-自动化部署WEB
- LeetCode -- Ugly Number II
- 信息学奥林匹克竞赛-计算机常用存储单位
- 将博客搬至CSDN
- ubuntu14.04,配置GOPATH(GO语言开发代码存放目录)
- Executor线程池解析
- JAVA Map List Set 的contans 方法
- 第五章Object-C类
- java存储时间date到mysql的datetime格式的方法
- 在R中使用支持向量机(SVM)进行数据挖掘(上)
- PendingIntent
- LeetCode -- Ugly Number
- PythonOS库练习——模拟linux文件管理命令
- 配置ruby环境