线程池有2种:ThreadPoolExecutor和ForkJoinPool。
一、ThreadPoolExecutor
Executors创建线程池的方式,阿里《java开发手册》禁用:
//1.Executors.newFixedThreadPool(5);
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());//队列容量是Integer.MAX_VALUE,任务太多可能导致OOM
}
//2、Executors.newSingleThreadExecutor();
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));//队列容量是Integer.MAX_VALUE,任务太多可能导致OOM
}
//Executors.newCachedThreadPool();
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,//最大线程数是Integer.MAX_VALUE,任务太多可能导致OOM
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());//队列容量为1,即所有任务即时执行
}
正确的创建线程池的方式:
ExecutorService executor = new ThreadPoolExecutor(2/*核心线程数,不会回收的*/, 8/*最大线程数*/,
10, TimeUnit.SECONDS/**闲置10S可以回收非核心线程/, new LinkedBlockingDeque<>(50)/*任务队列的容量*/,
new ThreadFactory() {//创建线程的工厂
final AtomicInteger integer = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "thread-pool-" + integer.getAndIncrement());
thread.setPriority(Thread.MIN_PRIORITY);//线程优先级低于UI线程最好
return thread;
}
},
/* If the task cannot be submitted for execution, either because this
* executor has been shutdown or because its capacity has been reached,
* the task is handled by the current {@code RejectedExecutionHandler}.
*/
new ThreadPoolExecutor.DiscardOldestPolicy()
);
executor.execute(new Runnable() {
@Override
public void run() {
// TODO: 2021/8/29 do some thing
}
});
线程池的构造方法:
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
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;
}
任务队列BlockingQueue:
有2种实现,链表实现和数组实现,前者一般是LinkedBlockingQueue(单向链表阻塞队列,引用头尾节点),后者一般是ArrayBlockingQueue(数组阻塞队列),前者节省内存,后者适用于需要按索引存取的情况,由于线程池的场景只需要队列头取尾存,所以都使用前者。
队列塞满任务时再次添加任务的拒绝策略RejectedExecutionHandler:
有4种实现,DiscardOldestPolicy--丢弃任务队列队首任务(最老的任务),再次尝试添加新任务(添加到队尾);CallerRunsPolicy--队列满了,本线程池不再提供服务,哪个线程在添加任务,就在哪个线程执行;AbortPolicy--队列满了,本线程池不再提供服务,再添加新任务就报异常RejectedExecutionException;DiscardPolicy--队列满了,本线程池不再提供服务,再添加新任务就当没看见。
threadPool.execute():
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
/*
* 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.
*/
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
/*
*
* 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.
*/
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);
}
/*
*
* 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.
*/
else if (!addWorker(command, false))
reject(command);
}
addWorker(command,false):
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();/*****新线程启动了,会执行Worker的run方法****/
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
Worker.run():
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;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker. */
public void run() {
runWorker(this);/*****调用ThreadPool的runWorker方法,这个方法跑在新线程中,内部是个循环******/
}
threadPool.runWorker():
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) {//循环从阻塞队列中获取任务,超时(比如我们设置的10S)、threadPool被shutDown则会返回null,将会在finally代码块中回收线程
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);//回收线程
}
}
threadPool.getTask():
private Runnable getTask() {//本方法跑在多线程中,任务队列(BlockingQueue是线程安全的)
boolean timedOut = false; // Did the last poll() time out?
for (;;) {//循环从阻塞队列中获取任务
int c = ctl.get();
int rs = runStateOf(c);
// Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
decrementWorkerCount();
return null;
}
int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
&& (wc > 1 || workQueue.isEmpty())) {
if (compareAndDecrementWorkerCount(c))
return null;
continue;
}
try {
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :/***超时时间内必须获取到任务,否则会返回null,线程闲置超时将被回收***/
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
二、ForkJoinPool
典型场景:
public class ExampleUnitTest {
static class SumCallable2 implements Callable<Long> {
long start, end;
public SumCallable2(long start, long end) {
this.start = start;
this.end = end;
}
@Override
public Long call() throws Exception {
long sum = 0;
for (long i = start; i < end + 1; i++) {
sum += i;
}
return sum;
}
}
/**本方法不应该跑在UI线程中**/
@Test
public void testForkJoinPool2() {
long start = 1, end = 10_0000_0000L;
//1.利用java的stream:从1加到10亿
Long sum = LongStream.rangeClosed(start, end).parallel().reduce(0, Long::sum);
System.out.println(sum);
//2.自己通过forkJointPool实现:从1加到10亿
ForkJoinPool forkJoinPool = new ForkJoinPool(5,//并行度,与线程数相关
ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
final int TASK_NUM = 50;//将任务划分成50个,先执行完自己队列中的任务的线程,会窃取别的线程的队列中的任务去执行
long k = end / TASK_NUM ;
List<Callable<Long>> tasks = new ArrayList<>(TASK_NUM);
for (int i = 0; i < TASK_NUM ; i++) {
tasks.add(new SumCallable2(i * k + 1, (i + 1) * k));
}
List<Future<Long>> futures = forkJoinPool.invokeAll(tasks);
AtomicLong atomicSum = new AtomicLong(0);
futures.forEach(longFuture -> {
try {
atomicSum.getAndAdd(longFuture.get());
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println(atomicSum);
}
}
输出结果:
2000000001000000000
1756
2000000001000000000
1919
作者Doug lea对ForkJoinPool解释:
A {@code ForkJoinPool} differs from other kinds of {@link * ExecutorService} mainly by virtue of employing work-stealing
区别于其它线程池的最大特点是:工作窃取带来的好处。
所以如果想使用ForkJoinPool,先想想你的那些任务是否能够工作窃取。如果不能,就该用ThreadPoolExecutor。需要工作窃取意味着任务多、各任务的耗时不同、无法给各线程均匀分摊任务,所以导致有的线程会先执行完,而有的线程还有多余任务未执行。像本例中计算1加到10亿,其实很好分摊,分摊成5个任务用5个线程执行(使用ThreadPoolExecutor)会更快。