Java线程池ThreadPoolExcutor任务分配、执⾏顺序
⼀、任务分配顺序
1、顺序
1. ⾸先会判断线程池中活跃线程数是否⼩于核⼼线程,如果是,则创建核⼼线程并将任务分配给核⼼线程
2. 如果此时仍然有任务提交,且活跃线程数⼤于等于核⼼线程数,则将任务添加到阻塞队列中
3. 如果阻塞队列放满了,还有任务继续提交,则创建⾮核⼼线程并分配任务。如果此时⾮核⼼线程分配完后还有任务提交,则触发拒绝
策略!。
2、源码
1public void execute(Runnable command) {
2 if (command == null)
3 throw new NullPointerException();
4 /*
5 * Proceed in 3 steps:
6 *
7 * 1. If fewer than corePoolSize threads are running, try to
8 * start a new thread with the given command as its first
9 * task. The call to addWorker atomically checks runState and
10 * workerCount, and so prevents fal alarms that would add
11 * threads when it shouldn't, by returning fal.
12 *
13 * 2. If a task can be successfully queued, then we still need
14 * to double-check whether we should have added a thread
bbc纪录片中英字幕15 * (becau existing ones died since last checking) or that
16 * the pool shut down since entry into this method. So we
17 * recheck state and if necessary roll back the enqueuing if
18 * stopped, or start a new thread if there are none.
19 *
20 * 3. If we cannot queue task, then we try to add a new
21 * thread. If it fails, we know we are shut down or saturated
22 * and so reject the task.
23 */
24 int c = ();工作室英文
janna
25 // 如果当前⼯作线程数少于核⼼线程数,创建新的worker
undefined是什么意思26 if (workerCountOf(c) < corePoolSize) {
27 if (addWorker(command, true))
28 return;
29 c = ();
30 }
31 // 如果当前线程是running的,并且从队列中成功拿出来任务
32 if (isRunning(c) && workQueue.offer(command)) {
33 int recheck = ();
34 if (! isRunning(recheck) && remove(command))
35 reject(command);
36 el if (workerCountOf(recheck) == 0)
37 addWorker(null, fal);
38 }
39 // 这⾥的fal,表⽰的是⾮核⼼线程,即会尝试着给⾮核⼼线程分配任务,如果失败,执⾏拒绝策略
40 el if (!addWorker(command, fal))
41 reject(command);tony abbott
42 }
3、图⽚
⼆、任务执⾏顺序
1、顺序
1. 核⼼线程
2. ⾮核⼼线程
3. 队列
2、源码
1final void runWorker(Worker w) {
2 Thread wt = Thread.currentThread();
3 Runnable task = w.firstTask;
4 w.firstTask = null;
5 w.unlock(); // allow interrupts
6 boolean completedAbruptly = true;
7 try {
8 while (task != null || (task = getTask()) != null) {
9 w.lock();
10 // If pool is stopping, ensure thread is interrupted;
疲乏
11 // if not, ensure thread is not interrupted. This
12 // requires a recheck in cond ca to deal with
13 // shutdownNow race while clearing interrupt
14 if (((), STOP) ||
15 (Thread.interrupted() &&
16 (), STOP))) &&
17 !wt.isInterrupted())
18 wt.interrupt();
19 try {
20 beforeExecute(wt, task);
21 Throwable thrown = null;
22 try {
23 task.run();
24 } catch (RuntimeException x) {
25 thrown = x; throw x;
26 } catch (Error x) {
27 thrown = x; throw x;
28 } catch (Throwable x) {
29 thrown = x; throw new Error(x);
30 } finally {
30 } finally {
31 afterExecute(task, thrown);
32 }
33 } finally {
34 task = null;
35 w.completedTasks++;
36 w.unlock();
37 }
38 }
vt是什么意思
39 completedAbruptly = fal;
40 } finally {
41 processWorkerExit(w, completedAbruptly);
42 }
43 }
44
45
46
47private Runnable getTask() {
48 boolean timedOut = fal; // Did the last poll() time out?
49
50 for (;;) {
51 int c = ();
52 int rs = runStateOf(c);
53
54 // Check if queue empty only if necessary.
55 if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
56 decrementWorkerCount();
57 return null;
capp58 }
59
60 int wc = workerCountOf(c);
61
62 // Are workers subject to culling?
63 boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
64
65 if ((wc > maximumPoolSize || (timed && timedOut))
66 && (wc > 1 || workQueue.isEmpty())) {
67 if (compareAndDecrementWorkerCount(c))
68 return null;
69 continue;
70 }
71
72 try {
73 Runnable r = timed ?
74 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
75 workQueue.take();
76 if (r != null)
77 return r;
78 timedOut = true;
79 } catch (InterruptedException retry) {翻译英语
80 timedOut = fal;
81 }
82 }
83 }
task:⾃⼰的任务
getTask(): 从队列中去取任务
即优先会从核⼼线程和⾮核⼼线程处去执⾏任务,直到它们为空的时候(当taks == null时),然后才会去从中拿任务。这就验证了执⾏顺序。
breakingnews