從原始碼角度分析建立執行緒池究竟有哪些方式

冰河團隊發表於2022-04-21

大家好,我是冰河~~

在Java的高併發領域,執行緒池一直是一個繞不開的話題。有些童鞋一直在使用執行緒池,但是,對於如何建立執行緒池僅僅停留在使用Executors工具類的方式,那麼,建立執行緒池究竟存在哪幾種方式呢?就讓我們一起從建立執行緒池的原始碼來深入分析究竟有哪些方式可以建立執行緒池。

使用Executors工具類建立執行緒池

在建立執行緒池時,初學者用的最多的就是Executors 這個工具類,而使用這個工具類建立執行緒池時非常簡單的,不需要關注太多的執行緒池細節,只需要傳入必要的引數即可。Executors 工具類提供了幾種建立執行緒池的方法,如下所示。

  • Executors.newCachedThreadPool:建立一個可快取的執行緒池,如果執行緒池的大小超過了需要,可以靈活回收空閒執行緒,如果沒有可回收執行緒,則新建執行緒
  • Executors.newFixedThreadPool:建立一個定長的執行緒池,可以控制執行緒的最大併發數,超出的執行緒會在佇列中等待
  • Executors.newScheduledThreadPool:建立一個定長的執行緒池,支援定時、週期性的任務執行
  • Executors.newSingleThreadExecutor: 建立一個單執行緒化的執行緒池,使用一個唯一的工作執行緒執行任務,保證所有任務按照指定順序(先入先出或者優先順序)執行
  • Executors.newSingleThreadScheduledExecutor:建立一個單執行緒化的執行緒池,支援定時、週期性的任務執行
  • Executors.newWorkStealingPool:建立一個具有並行級別的work-stealing執行緒池

其中,Executors.newWorkStealingPool方法是Java 8中新增的建立執行緒池的方法,它能夠為執行緒池設定並行級別,具有更高的併發度和效能。除了此方法外,其他建立執行緒池的方法本質上呼叫的是ThreadPoolExecutor類的構造方法。

例如,我們可以使用如下程式碼建立執行緒池。

Executors.newWorkStealingPool();
Executors.newCachedThreadPool();
Executors.newScheduledThreadPool(3);

使用ThreadPoolExecutor類建立執行緒池

從程式碼結構上看ThreadPoolExecutor類繼承自AbstractExecutorService,也就是說,ThreadPoolExecutor類具有AbstractExecutorService類的全部功能。

既然Executors工具類中建立執行緒池大部分呼叫的都是ThreadPoolExecutor類的構造方法,所以,我們也可以直接呼叫ThreadPoolExecutor類的構造方法來建立執行緒池,而不再使用Executors工具類。接下來,我們一起看下ThreadPoolExecutor類的構造方法。

ThreadPoolExecutor類中的所有構造方法如下所示。

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.acc = System.getSecurityManager() == null ?
			null :
			AccessController.getContext();
	this.corePoolSize = corePoolSize;
	this.maximumPoolSize = maximumPoolSize;
	this.workQueue = workQueue;
	this.keepAliveTime = unit.toNanos(keepAliveTime);
	this.threadFactory = threadFactory;
	this.handler = handler;
}

由ThreadPoolExecutor類的構造方法的原始碼可知,建立執行緒池最終呼叫的構造方法如下。

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.acc = System.getSecurityManager() == null ?
			null :
			AccessController.getContext();
	this.corePoolSize = corePoolSize;
	this.maximumPoolSize = maximumPoolSize;
	this.workQueue = workQueue;
	this.keepAliveTime = unit.toNanos(keepAliveTime);
	this.threadFactory = threadFactory;
	this.handler = handler;
}

關於此構造方法中各引數的含義和作用,各位可以移步《高併發之——不得不說的執行緒池與ThreadPoolExecutor類淺析》進行查閱。

大家可以自行呼叫ThreadPoolExecutor類的構造方法來建立執行緒池。例如,我們可以使用如下形式建立執行緒池。

new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                       60L, TimeUnit.SECONDS,
                       new SynchronousQueue<Runnable>());

使用ForkJoinPool類建立執行緒池

在Java8的Executors工具類中,新增瞭如下建立執行緒池的方式。

public static ExecutorService newWorkStealingPool(int parallelism) {
	return new ForkJoinPool
		(parallelism,
		 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
		 null, true);
}

public static ExecutorService newWorkStealingPool() {
	return new ForkJoinPool
		(Runtime.getRuntime().availableProcessors(),
		 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
		 null, true);
}

從原始碼可以可以,本質上呼叫的是ForkJoinPool類的構造方法類建立執行緒池,而從程式碼結構上來看ForkJoinPool類繼承自AbstractExecutorService抽象類。接下來,我們看下ForkJoinPool類的構造方法。

public ForkJoinPool() {
	this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()),
		 defaultForkJoinWorkerThreadFactory, null, false);
}
 public ForkJoinPool(int parallelism) {
	this(parallelism, defaultForkJoinWorkerThreadFactory, null, false);
}

public ForkJoinPool(int parallelism,
				ForkJoinWorkerThreadFactory factory,
				UncaughtExceptionHandler handler,
				boolean asyncMode) {
	this(checkParallelism(parallelism),
		 checkFactory(factory),
		 handler,
		 asyncMode ? FIFO_QUEUE : LIFO_QUEUE,
		 "ForkJoinPool-" + nextPoolId() + "-worker-");
	checkPermission();
}

private ForkJoinPool(int parallelism,
				 ForkJoinWorkerThreadFactory factory,
				 UncaughtExceptionHandler handler,
				 int mode,
				 String workerNamePrefix) {
	this.workerNamePrefix = workerNamePrefix;
	this.factory = factory;
	this.ueh = handler;
	this.config = (parallelism & SMASK) | mode;
	long np = (long)(-parallelism); // offset ctl counts
	this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
}

通過檢視原始碼得知,ForkJoinPool的構造方法,最終呼叫的是如下私有構造方法。

private ForkJoinPool(int parallelism,
				 ForkJoinWorkerThreadFactory factory,
				 UncaughtExceptionHandler handler,
				 int mode,
				 String workerNamePrefix) {
	this.workerNamePrefix = workerNamePrefix;
	this.factory = factory;
	this.ueh = handler;
	this.config = (parallelism & SMASK) | mode;
	long np = (long)(-parallelism); // offset ctl counts
	this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK);
}

其中,各引數的含義如下所示。

  • parallelism:併發級別。
  • factory:建立執行緒的工廠類物件。
  • handler:當執行緒池中的執行緒丟擲未捕獲的異常時,統一使用UncaughtExceptionHandler物件處理。
  • mode:取值為FIFO_QUEUE或者LIFO_QUEUE。
  • workerNamePrefix:執行任務的執行緒名稱的字首。

當然,私有構造方法雖然是引數最多的一個方法,但是其不會直接對外方法,我們可以使用如下方式建立執行緒池。

new ForkJoinPool();
new ForkJoinPool(Runtime.getRuntime().availableProcessors());
new ForkJoinPool(Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);

使用ScheduledThreadPoolExecutor類建立執行緒池

在Executors工具類中存在如下方法類建立執行緒池。

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
	return new DelegatedScheduledExecutorService
		(new ScheduledThreadPoolExecutor(1));
}

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
	return new DelegatedScheduledExecutorService
		(new ScheduledThreadPoolExecutor(1, threadFactory));
}

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
	return new ScheduledThreadPoolExecutor(corePoolSize);
}

public static ScheduledExecutorService newScheduledThreadPool(
		int corePoolSize, ThreadFactory threadFactory) {
	return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}

從原始碼來看,這幾個方法本質上呼叫的都是ScheduledThreadPoolExecutor類的構造方法,ScheduledThreadPoolExecutor中存在的構造方法如下所示。

public ScheduledThreadPoolExecutor(int corePoolSize) {
	super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
		  new DelayedWorkQueue());
}

public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {
	super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
		  new DelayedWorkQueue(), threadFactory);
}

public ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) {
	super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
		  new DelayedWorkQueue(), handler);
}

public ScheduledThreadPoolExecutor(int corePoolSize,ThreadFactory threadFactory, RejectedExecutionHandler handler) {
	super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
		  new DelayedWorkQueue(), threadFactory, handler);
}

而從程式碼結構上看,ScheduledThreadPoolExecutor類繼承自ThreadPoolExecutor類,本質上還是呼叫ThreadPoolExecutor類的構造方法,只不過此時傳遞的佇列為DelayedWorkQueue。我們可以直接呼叫ScheduledThreadPoolExecutor類的構造方法來建立執行緒池,例如以如下形式建立執行緒池。

new ScheduledThreadPoolExecutor(3)

好了,今天就到這兒吧,我是冰河,我們下期見~~

相關文章