源码Executor
来源:互联网 发布:宇众网络 编辑:程序博客网 时间:2024/06/10 19:05
比较重要的几个类:
ExecutorService
真正的线程池接口。
ScheduledExecutorService
能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。
ThreadPoolExecutor
ExecutorService的默认实现。
ScheduledThreadPoolExecutor
继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//表明各种状态 private volatile int state; private static final int NEW = 0; private static final int COMPLETING = 1; private static final int NORMAL = 2; private static final int EXCEPTIONAL = 3; private static final int CANCELLED = 4; private static final int INTERRUPTING = 5; private static final int INTERRUPTED = 6;
public <T> Future<T> submit(Callable<T> task) { if (task == null) throw new NullPointerException(); RunnableFuture<T> ftask = newTaskFor(task); execute(ftask);//最终还是调用execute() return ftask; }
</pre><pre name="code" class="java">
public void execute(Runnable command) { if (command == null) throw new NullPointerException(); /* * Proceed in 3 steps: * * 1. If fewer than corePoolSize threads are running, try to * start a new thread with the given command as its first * task. The call to addWorker atomically checks runState and * workerCount, and so prevents false alarms that would add * threads when it shouldn't, by returning false. * * 2. If a task can be successfully queued, then we still need * to double-check whether we should have added a thread * (because existing ones died since last checking) or that * the pool shut down since entry into this method. So we * recheck state and if necessary roll back the enqueuing if * stopped, or start a new thread if there are none. * * 3. If we cannot queue task, then we try to add a new * thread. If it fails, we know we are shut down or saturated * and so reject the task. */ int c = ctl.get(); if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true))//线程的运行在addWorker()方法中 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); }
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 { final ReentrantLock mainLock = this.mainLock; w = new Worker(firstTask); final Thread t = w.thread; if (t != null) { mainLock.lock(); try { // Recheck while holding lock. // Back out on ThreadFactory failure or if // shut down before lock acquired. int c = ctl.get(); int rs = runStateOf(c); 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; }
public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L); //阻塞 return report(s); }
public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L); //阻塞 return report(s); }
//将runnable的对象包装成一个Callable对象,并返回result<pre name="code" class="java">public FutureTask(Runnable runnable, V result) { this.callable = Executors.callable(runnable, result); this.state = NEW; // ensure visibility of callable }
0 0
- 源码Executor
- Executor源码分析
- ExecutorService&Executor接口源码
- # Executor源码分析
- worker启动executor源码分析-executor.clj
- 源码-stage->task->taskSet->executor
- JDK7中Executor源码概述
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- Executor
- java.util.concurrent源码学习系列--Executor
- DeltasFeatureExtractor API 及特征提取
- android滑动菜单demo
- C#中用后台代码实现向XML文档中追加内容
- Linux下常见压缩解压命令
- linux命令详解——tar
- 源码Executor
- C++代码规范
- 谈谈 Python 类
- C/C学习笔记/结构
- UIEvent 基本学习
- [BI项目记]-搭建代码管理环境之签入代码
- ViewPager实现页面切换
- 【Science阅读笔记】AI-Poker: Solving Imperfect-Information Games
- Android学习 - 移动web开发框架