線程池
今天我們來看看JDK給我們提供的默認的線程池的實現。
ThreadPoolExecutor:我們通常所說的線程池。多個線程共享同一個任務隊列。
- SingleThreadPool
- CachedPool
- FixedThreadPool
- ScheduledPool
ForkJoinPoll:先將任務分解,最後再彙總。每個線程有自己的任務隊列。
- WorkStealingPool
SingleThreadPool
SingleThreadPool 這個線程池裏面只有一個線程。這樣可以保證 我們扔進去的任務是被順序執行的。
問:爲什麼會有單線程的線程池?
單線程的線程池是有任務隊列的;線程池能幫你提供線程生命週期的管理。
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class T07_SingleThreadPool {
public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor();
for (int i = 0; i < 5; i++) {
final int j = i;
service.execute(() -> {
System.out.println(j + " " + Thread.currentThread().getName());
});
}
}
}
CachedPool
CachedPool 核心線程數爲0
,最大線程數是Integer.MAX_VALUE
SynchronousQueue
是一個特殊的隊列:
SynchronousQueue 是一個阻塞隊列,其中每個插入操作必須等待另一個線程執行相應的刪除操作,反之亦然。同步隊列沒有任何內部容量,甚至容量都不是1。您無法查看同步隊列,因爲只有當您試圖刪除某個元素時,它纔會出現;你不能插入元素(使用任何方法),除非另一個線程試圖刪除它;你不能迭代,因爲沒有東西可以迭代。隊列的頭是第一個排隊插入線程試圖添加到隊列中的元素;如果沒有這樣的排隊線程,那麼就沒有可刪除的元素,poll()將返回null。對於其他集合方法(例如包含),SynchronousQueue充當空集合。此隊列不允許空元素。
CachedPool 這個線程池,當任務到來時,如果有線程空閒,我就用現有的線程;如果所有線程忙,就啓動一個新線程。
阿里不推薦使用這種線程池!
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class T08_CachedPool {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
System.out.println(service);
for (int i = 0; i < 2; i++) {
service.execute(() -> {
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
}
System.out.println(service);
TimeUnit.SECONDS.sleep(80);
System.out.println(service);
}
}
運行結果
java.util.concurrent.ThreadPoolExecutor@6aaa5eb0[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
java.util.concurrent.ThreadPoolExecutor@6aaa5eb0[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
pool-1-thread-1
pool-1-thread-2
java.util.concurrent.ThreadPoolExecutor@6aaa5eb0[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]
Process finished with exit code 0
FixedThreadPool
FixedThreadPool 是一個固定線程數的線程池。
它的核心線程數和最大線程數是固定的!
固定線程數的好處是:
適合做一些並行的計算,比如你要找1-200000之內所有的質數,你將這個大任務拆成4個小線程,共同去運行。
利用線程池進行並行的計算,肯定比串行計算要更快。
併發和並行的區別:
併發指任務提交,並行指任務執行;
並行是多個CPU同時處理,併發是多個任務同時過來;
並行是併發的子集
/**
* 線程池的概念
* nasa
*/
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class T09_FixedThreadPool {
public static void main(String[] args) throws InterruptedException, ExecutionException {
long start = System.currentTimeMillis();
getPrime(1, 200000);
long end = System.currentTimeMillis();
System.out.println(end - start);
final int cpuCoreNum = 4;
ExecutorService service = Executors.newFixedThreadPool(cpuCoreNum);
MyTask t1 = new MyTask(1, 80000); //1-5 5-10 10-15 15-20
MyTask t2 = new MyTask(80001, 130000);
MyTask t3 = new MyTask(130001, 170000);
MyTask t4 = new MyTask(170001, 200000);
Future<List<Integer>> f1 = service.submit(t1);
Future<List<Integer>> f2 = service.submit(t2);
Future<List<Integer>> f3 = service.submit(t3);
Future<List<Integer>> f4 = service.submit(t4);
start = System.currentTimeMillis();
f1.get();
f2.get();
f3.get();
f4.get();
end = System.currentTimeMillis();
System.out.println(end - start);
}
static class MyTask implements Callable<List<Integer>> {
int startPos, endPos;
MyTask(int s, int e) {
this.startPos = s;
this.endPos = e;
}
@Override
public List<Integer> call() throws Exception {
List<Integer> r = getPrime(startPos, endPos);
return r;
}
}
static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return false;
}
return true;
}
static List<Integer> getPrime(int start, int end) {
List<Integer> results = new ArrayList<>();
for (int i = start; i <= end; i++) {
if (isPrime(i)) results.add(i);
}
return results;
}
}
串行和並行計算耗時比較:
8860
3039
CachedThreadPool 和 FixedThreadPool 的選用
如果任務來的速度忽快忽慢,但是我要保證任務來的時候有人來做這個任務,那麼我們可以使用 CachedThreadPool
,保證任務不會堆積。
如果任務來的比較平穩,我們大概估算出一個需要的線程數量,這個值完全能夠處理所有的任務,就可以使用FixedThreadPool
阿里是這兩種都不用,自己估算,進行精確定義。
ScheduledPool
專門用來執行定時任務的一個線程池。
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class T10_ScheduledPool {
public static void main(String[] args) {
ScheduledExecutorService service = Executors.newScheduledThreadPool(4);
service.scheduleAtFixedRate(() -> {
try {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}, 0, 500, TimeUnit.MILLISECONDS);
}
}
自定義拒絕策略
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.concurrent.*;
public class T14_MyRejectedHandler {
public static void main(String[] args) {
ExecutorService service = new ThreadPoolExecutor(4, 4,
0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(6),
Executors.defaultThreadFactory(),
new MyHandler());
}
static class MyHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//log("r rejected")
//save r kafka mysql redis
//try 3 times
if (executor.getQueue().size() < 10000) {
//try put again();
}
}
}
}
ThreadPoolExecutor源碼解析
1、常用變量的解釋
// 1. `ctl`,可以看做一個int類型的數字,高3位表示線程池狀態,低29位表示worker數量
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
// 2. `COUNT_BITS`,`Integer.SIZE`爲32,所以`COUNT_BITS`爲29
private static final int COUNT_BITS = Integer.SIZE - 3;
// 3. `CAPACITY`,線程池允許的最大線程數。1左移29位,然後減1,即爲 2^29 - 1
private static final int CAPACITY = (1 << COUNT_BITS) - 1;
// runState is stored in the high-order bits
// 4. 線程池有5種狀態,按大小排序如下:RUNNING < SHUTDOWN < STOP < TIDYING < TERMINATED
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
// Packing and unpacking ctl
// 5. `runStateOf()`,獲取線程池狀態,通過按位與操作,低29位將全部變成0
private static int runStateOf(int c) { return c & ~CAPACITY; }
// 6. `workerCountOf()`,獲取線程池worker數量,通過按位與操作,高3位將全部變成0
private static int workerCountOf(int c) { return c & CAPACITY; }
// 7. `ctlOf()`,根據線程池狀態和線程池worker數量,生成ctl值
private static int ctlOf(int rs, int wc) { return rs | wc; }
/*
* Bit field accessors that don't require unpacking ctl.
* These depend on the bit layout and on workerCount being never negative.
*/
// 8. `runStateLessThan()`,線程池狀態小於xx
private static boolean runStateLessThan(int c, int s) {
return c < s;
}
// 9. `runStateAtLeast()`,線程池狀態大於等於xx
private static boolean runStateAtLeast(int c, int s) {
return c >= s;
}
2、構造方法
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;
// 根據傳入參數`unit`和`keepAliveTime`,將存活時間轉換爲納秒存到變量`keepAliveTime `中
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
3、提交執行task的過程
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();
// worker數量比核心線程數小,直接創建worker執行任務
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
// worker數量超過核心線程數,任務直接進入隊列
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
// 線程池狀態不是RUNNING狀態,說明執行過shutdown命令,需要對新加入的任務執行reject()操作。
// 這兒爲什麼需要recheck,是因爲任務入隊列前後,線程池的狀態可能會發生變化。
if (! isRunning(recheck) && remove(command))
reject(command);
// 這兒爲什麼需要判斷0值,主要是在線程池構造方法中,核心線程數允許爲0
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
// 如果線程池不是運行狀態,或者任務進入隊列失敗,則嘗試創建worker執行任務。
// 這兒有3點需要注意:
// 1. 線程池不是運行狀態時,addWorker內部會判斷線程池狀態
// 2. addWorker第2個參數表示是否創建核心線程
// 3. addWorker返回false,則說明任務執行失敗,需要執行reject操作
else if (!addWorker(command, false))
reject(command);
}
4、addworker源碼解析
private boolean addWorker(Runnable firstTask, boolean core) {
retry:
// 外層自旋
for (;;) {
int c = ctl.get();
int rs = runStateOf(c);
// 這個條件寫得比較難懂,我對其進行了調整,和下面的條件等價
// (rs > SHUTDOWN) ||
// (rs == SHUTDOWN && firstTask != null) ||
// (rs == SHUTDOWN && workQueue.isEmpty())
// 1. 線程池狀態大於SHUTDOWN時,直接返回false
// 2. 線程池狀態等於SHUTDOWN,且firstTask不爲null,直接返回false
// 3. 線程池狀態等於SHUTDOWN,且隊列爲空,直接返回false
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN &&
! (rs == SHUTDOWN &&
firstTask == null &&
! workQueue.isEmpty()))
return false;
// 內層自旋
for (;;) {
int wc = workerCountOf(c);
// worker數量超過容量,直接返回false
if (wc >= CAPACITY ||
wc >= (core ? corePoolSize : maximumPoolSize))
return false;
// 使用CAS的方式增加worker數量。
// 若增加成功,則直接跳出外層循環進入到第二部分
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;
// worker的添加必須是串行的,因此需要加鎖
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)) {
// worker已經調用過了start()方法,則不再創建worker
if (t.isAlive()) // precheck that t is startable
throw new IllegalThreadStateException();
// worker創建並添加到workers成功
workers.add(w);
// 更新`largestPoolSize`變量
int s = workers.size();
if (s > largestPoolSize)
largestPoolSize = s;
workerAdded = true;
}
} finally {
mainLock.unlock();
}
// 啓動worker線程
if (workerAdded) {
t.start();
workerStarted = true;
}
}
} finally {
// worker線程啓動失敗,說明線程池狀態發生了變化(關閉操作被執行),需要進行shutdown相關操作
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
5、線程池worker任務單元
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
{
/**
* This class will never be serialized, but we provide a
* serialVersionUID to suppress a javac warning.
*/
private static final long serialVersionUID = 6138294804551838833L;
/** Thread this worker is running in. Null if factory fails. */
final Thread thread;
/** Initial task to run. Possibly null. */
Runnable firstTask;
/** Per-thread task counter */
volatile long completedTasks;
/**
* Creates with given first task and thread from ThreadFactory.
* @param firstTask the first task (null if none)
*/
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
// 這兒是Worker的關鍵所在,使用了線程工廠創建了一個線程。傳入的參數爲當前worker
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
// 省略代碼...
}
6、核心線程執行邏輯-runworker
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
// 調用unlock()是爲了讓外部可以中斷
w.unlock(); // allow interrupts
// 這個變量用於判斷是否進入過自旋(while循環)
boolean completedAbruptly = true;
try {
// 這兒是自旋
// 1. 如果firstTask不爲null,則執行firstTask;
// 2. 如果firstTask爲null,則調用getTask()從隊列獲取任務。
// 3. 阻塞隊列的特性就是:當隊列爲空時,當前線程會被阻塞等待
while (task != null || (task = getTask()) != null) {
// 這兒對worker進行加鎖,是爲了達到下面的目的
// 1. 降低鎖範圍,提升性能
// 2. 保證每個worker執行的任務是串行的
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();
// 執行任務,且在執行前後通過`beforeExecute()`和`afterExecute()`來擴展其功能。
// 這兩個方法在當前類裏面爲空實現。
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 {
// 幫助gc
task = null;
// 已完成任務數加一
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
// 自旋操作被退出,說明線程池正在結束
processWorkerExit(w, completedAbruptly);
}
}
WorkStealingPool
原來的線程池:有一個線程的集合,去一個任務隊列裏面取任務,取出任務之後執行。下圖:
WorkStealingPool 偷任務的線程池:每一個線程都有自己獨立的任務隊列,如果某一個線程執行完自己的任務之後,要去別的線程那裏偷任務,分擔別的線程的任務。
WorkStealingPool 本質上還是一個 ForkJoinPool
/**
*
*/
package com.mashibing.juc.c_026_01_ThreadPool;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class T11_WorkStealingPool {
public static void main(String[] args) throws IOException {
ExecutorService service = Executors.newWorkStealingPool();
System.out.println(Runtime.getRuntime().availableProcessors());
service.execute(new R(1000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000)); //daemon
service.execute(new R(2000));
//由於產生的是精靈線程(守護線程、後臺線程),主線程不阻塞的話,看不到輸出
System.in.read();
}
static class R implements Runnable {
int time;
R(int t) {
this.time = t;
}
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(time + " " + Thread.currentThread().getName());
}
}
}
ForkJoinPool
把大任務切分成一個個小任務去運行,執行完之後進行彙總。
可以有返回值或無返回值。
package com.mashibing.juc.c_026_01_ThreadPool;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;
public class T12_ForkJoinPool {
static int[] nums = new int[1000000];
static final int MAX_NUM = 50000;
static Random r = new Random();
static {
for (int i = 0; i < nums.length; i++) {
nums[i] = r.nextInt(100);
}
System.out.println("stream api---" + Arrays.stream(nums).sum()); //stream api,單線程的計算方式
}
static class AddTask extends RecursiveAction {
int start, end;
AddTask(int s, int e) {
start = s;
end = e;
}
@Override
protected void compute() {
if (end - start <= MAX_NUM) {
long sum = 0L;
for (int i = start; i < end; i++) sum += nums[i];
System.out.println("from:" + start + " to:" + end + " = " + sum);
} else {
int middle = start + (end - start) / 2;
AddTask subTask1 = new AddTask(start, middle);
AddTask subTask2 = new AddTask(middle, end);
subTask1.fork();
subTask2.fork();
}
}
}
// 帶有返回值的任務拆分
static class AddTaskReturn extends RecursiveTask<Long> {
private static final long serialVersionUID = 1L;
int start, end;
AddTaskReturn(int s, int e) {
start = s;
end = e;
}
@Override
protected Long compute() {
if (end - start <= MAX_NUM) {
long sum = 0L;
for (int i = start; i < end; i++) sum += nums[i];
return sum;
}
int middle = start + (end - start) / 2;
AddTaskReturn subTask1 = new AddTaskReturn(start, middle);
AddTaskReturn subTask2 = new AddTaskReturn(middle, end);
subTask1.fork();
subTask2.fork();
return subTask1.join() + subTask2.join();
}
}
public static void main(String[] args) throws IOException {
/*ForkJoinPool fjp = new ForkJoinPool();
AddTask task = new AddTask(0, nums.length);
fjp.execute(task);*/
T12_ForkJoinPool temp = new T12_ForkJoinPool();
ForkJoinPool fjp = new ForkJoinPool();
AddTaskReturn task = new AddTaskReturn(0, nums.length);
fjp.execute(task);
long result = task.join();
System.out.println(result);
//System.in.read();
}
}
流式API的ForkJoinPool的算法實現
流式API的底層也是使用 ForkJoinPool 來實現的。nums.parallelStream().forEach
這種並行流處理起來效率會更高一些。
package com.mashibing.juc.c_026_01_ThreadPool;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class T13_ParallelStreamAPI {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 10000; i++) nums.add(1000000 + r.nextInt(1000000));
//System.out.println(nums);
long start = System.currentTimeMillis();
nums.forEach(v -> isPrime(v));
long end = System.currentTimeMillis();
System.out.println(end - start);
//使用parallel stream api
start = System.currentTimeMillis();
nums.parallelStream().forEach(T13_ParallelStreamAPI::isPrime);
end = System.currentTimeMillis();
System.out.println(end - start);
}
static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return false;
}
return true;
}
}
OK,到此爲止,我們的線程池講完了!