一般的使用方式:这里不用Executors帮助类,而是构建自己的线程池:
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2016/7/21.
*/
public class ThreadPoolUtil {
private static ThreadPoolUtil threadPoolUtil;
private static ThreadPoolExecutor poolExecutor;
//核心线程数
private int corePoolSize = 5;
//最大线程数据
private int maximumPoolSize = 5;
private ThreadPoolUtil() {
poolExecutor = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
0L,
TimeUnit.SECONDS,
new LinkedBlockingQueue());
}
public static ThreadPoolUtil newInstance() {
if (threadPoolUtil == null) {
synchronized (ThreadPoolUtil.class) {
if (threadPoolUtil == null) {
threadPoolUtil = new ThreadPoolUtil();
}
}
}
return threadPoolUtil;
}
public void execute(Runnable task) {
poolExecutor.execute(task);
}
/**
* 执行需要返回结果的任务
*/
public Future execute(Callable task) {
return poolExecutor.submit(task);
}
}
简单的使用方式:
ThreadPoolUtil poolUtil = ThreadPoolUtil.newInstance();
poolUtil.execute(new Runnable() {...});
poolUtil.execute(new Runnable() {...});
poolUtil.execute(new Runnable() {...});
.
.
.
首先了解下构造函数的各个参数含义:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
corePoolSize : 核心线程数,也就是线程池中保持存活的线程有多少,包括正在执行任务和空闲的。
maximumPoolSize:最大线程数,也就是核心线程数全部都在执行任务,没有空闲的出来,且等待队列也已经装载满了,然后后又有新的任务需要执行,这时就会开启新的线程来执行,但总线程数:corePoolSize+newThreadSize <= maximumPoolSize。
keepAliveTime:多出的空闲线程存活的时间,也就是当前线程数大于核心线程数的这部分: nowThreadSize(当前线程总数) - corePoolSize 这部分的线程。
unit :线程存活的时间单位:有秒、分、时等等
workQueue :存放等待执行的任务队列
threadFactory :创建线程的工厂类、一般使用默认Executors.defaultThreadFactory()。
handler:处理多出的任务策略,也就是等待队列已满,最大线程数量中都没有空闲线程,此时还是有任务提交过来,就需要进行处理。有抛异常、不处理等。
提出问题:既然已经规定了核心线程数和最大线程数,当我们不断向线程池提交任务的时候,内部是否会进行开启核心线程数的线程进行并行执行任务,当再有任务提交的时候,就会放到等待队列中,直到队列满了,再开启线程来执行,当线程数据大于maximumPoolSize时,是否进行handler策略处理。
代码验证四部曲:
1.当需执行任务数小于核心线程数,池中的线程是否一开始就维持核心线程数的数量。
2.当执行任务数大于核心线程数,该任务是暂存在队列中还是立即开启新的线程执行。
3.当池中线程数量达到最大线程数时,且无空闲线程,队列也已经满了,此时还有新的任务提交过来,是否执行处理策略。
首先看已修改的类:
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by Administrator on 2016/7/21.
*/
public class ThreadPoolUtil {
private static ThreadPoolUtil threadPoolUtil;
private static ThreadPoolExecutor poolExecutor;
//核心线程数
private int corePoolSize = 2;
//最大线程数据
private int maximumPoolSize = 5;
//创建等待队列大小而2
private LinkedBlockingQueue queue = new LinkedBlockingQueue<>(2);
private ThreadPoolUtil() {
poolExecutor = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
0L,
TimeUnit.SECONDS,
queue,
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
}
public static ThreadPoolUtil newInstance() {
if (threadPoolUtil == null) {
synchronized (ThreadPoolUtil.class) {
if (threadPoolUtil == null) {
threadPoolUtil = new ThreadPoolUtil();
}
}
}
return threadPoolUtil;
}
public void execute(Runnable task) {
poolExecutor.execute(task);
}
/**
* 执行需要返回结果的任务
*/
public Future execute(Callable task) {
return poolExecutor.submit(task);
}
/**
* 获取当前执行任务的线程数
* @return
*/
public int getCurrentThreadNum(){
return poolExecutor.getActiveCount();
}
/**
* 获取当前等待任务数
*/
public int getCurrentWaittingTaskNum(){
return queue.size();
}
/**
* 获取池中线程数量
*/
public int getPoolSize(){
return poolExecutor.getPoolSize();
}
}
这里规定核心线程数为2、最大线程数为5、且等待队列的大小只能存放2个任务数的队列,使用了默认的线程工厂类和使用抛异常的处理策略:
AbortPolicy类
/**
* A handler for rejected tasks that throws a
* {@code RejectedExecutionException}.
*/
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
Task类:
class MyTask implements Runnable {
private String name;
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
public MyTask(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name + " is running --> time = " + df.format(new Date()));
try {
Thread.sleep(10000);//10s
System.out.println(name + " is end--> time = " + df.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return name;
}
}
验证1:
public static void main(String[] args) {
ThreadPoolUtil poolUtil = ThreadPoolUtil.newInstance();
poolUtil.execute(new MyTask("task1"));
System.out.println("当前执行任务数为:" + poolUtil.getActiveCount());
System.out.println("池中线程数:" + poolUtil.getPoolSize());
System.out.println("当前等待执行任务数为:" + poolUtil.getCurrentWaittingTaskNum());
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("--------------------------------------------");
System.out.println("当前执行任务数为:" + poolUtil.getActiveCount());
System.out.println("池中线程数:" + poolUtil.getPoolSize());
System.out.println("当前等待执行任务数为:" + poolUtil.getCurrentWaittingTaskNum());
}
只提交一个任务的结果:
poolUtil.execute(new MyTask(“task1”));
当前执行任务数为:1
task1 is running --> time = 14:13:45
池中线程数:1
当前等待执行任务数为:0
task1 is end--> time = 14:13:55
--------------------------------------------
当前执行任务数为:0
池中线程数:1
当前等待执行任务数为:0
提交2个任务的结果:
poolUtil.execute(new MyTask(“task1”));
poolUtil.execute(new MyTask(“task2”));
当前执行任务数为:2
池中线程数:2
task1 is running --> time = 14:15:25
task2 is running --> time = 14:15:25
当前等待执行任务数为:0
task2 is end--> time = 14:15:35
task1 is end--> time = 14:15:35
--------------------------------------------
当前执行任务数为:0
池中线程数:2
当前等待执行任务数为:0
结论:主要看池中的线程数,在规定核心线程数量下,并不是创建线程池然后内部就自动创建相应数量的线程,而是根据提交任务来创建,等任务执行完后才会保持规定的核心线程数的数量。
验证2:
提交4个任务:
poolUtil.execute(new MyTask(“task1”));
poolUtil.execute(new MyTask(“task2”));
poolUtil.execute(new MyTask(“task3”));
poolUtil.execute(new MyTask(“task4”));
task1 is running --> time = 14:23:25
task2 is running --> time = 14:23:25
当前执行任务数为:2
池中线程数:2
当前等待执行任务数为:2
task1 is end--> time = 14:23:35
task2 is end--> time = 14:23:35
task3 is running --> time = 14:23:35
task4 is running --> time = 14:23:35
--------------------------------------------
当前执行任务数为:2
池中线程数:2
当前等待执行任务数为:0
task4 is end--> time = 14:23:45
task3 is end--> time = 14:23:45
可以看到线程池中还是维持着2个线程数,由于核心线程数为2,所以多余的任务会存放在队列中,等待执行。
当执行5个任务时:
提交4个任务:
poolUtil.execute(new MyTask(“task1”));
poolUtil.execute(new MyTask(“task2”));
poolUtil.execute(new MyTask(“task3”));
poolUtil.execute(new MyTask(“task4”));
poolUtil.execute(new MyTask(“task5”));
task1 is running --> time = 14:30:06
task2 is running --> time = 14:30:06
当前执行任务数为:3
task5 is running --> time = 14:30:06
池中线程数:3
当前等待执行任务数为:2
task2 is end--> time = 14:30:16
task5 is end--> time = 14:30:16
task1 is end--> time = 14:30:16
task3 is running --> time = 14:30:16
task4 is running --> time = 14:30:16
--------------------------------------------
当前执行任务数为:2
池中线程数:2
当前等待执行任务数为:0
task3 is end--> time = 14:30:26
task4 is end--> time = 14:30:26
可以看到当等待队列满时,才会开启新的线程执行任务。
结论:当提交的任务数大于核心线程数时,会把任务放入到等待队列中,而不是开启新的线程执行,而是等队列满了,才会开启新的线程执行,而且池中的线程数也会恢复到核心线程数量,因为我们规定了keepAliveTime为0。
验证3:
提交9个任务:
poolUtil.execute(new MyTask(“task1”));
poolUtil.execute(new MyTask(“task2”));
poolUtil.execute(new MyTask(“task3”));
poolUtil.execute(new MyTask(“task4”));
poolUtil.execute(new MyTask(“task5”));
poolUtil.execute(new MyTask(“task6”));
poolUtil.execute(new MyTask(“task7”));
poolUtil.execute(new MyTask(“task8”));
poolUtil.execute(new MyTask(“task9”));
结果:
task1 is running –> time = 14:36:04
task5 is running –> time = 14:36:04
task2 is running –> time = 14:36:04
task6 is running –> time = 14:36:04
task7 is running –> time = 14:36:04
Exception in thread “main” java.util.concurrent.RejectedExecutionException:
Task task8 rejected from
java.util.concurrent.ThreadPoolExecutor@45ee12a7[Running, pool size = 5, active threads = 5, queued tasks = 2, completed tasks = 0]
task5 is end–> time = 14:36:14
task2 is end–> time = 14:36:14
task1 is end–> time = 14:36:14
task4 is running –> time = 14:36:14
task6 is end–> time = 14:36:14
task3 is running –> time = 14:36:14
task7 is end–> time = 14:36:14
task4 is end–> time = 14:36:24
task3 is end–> time = 14:36:24
可以看到task8提交时抛异常了,因为我们规定了核心线程数时2,最大线程数时5,等待队列的大小是2也就是说最多可以容纳7个任务数,当再提交任务时则就执行处理策略了,因为使用抛异常的处理策略。
结论:超过了最大线程数+队列的容量 的任务数则会执行处理策略。
源码探究:
从ThreadPoolExecutor类的execute方法入口:
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))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {//从这里可以看到如果任务数大于核心线程数就会执行到这里,这里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);
}
接下来进入addWorker方法查看线程是如何被创建和执行任务的:
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); //《1》创建一个Worker如要任务作为参数
final Thread t = w.thread; //《2》从里面得到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();//《3》开启线程
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
上面代码比较长,我们先不管各种条件的判断和加锁解锁的情况,从上面的注释我们可以看出先通过创建Worker对象,然后得到Thread对象,通过start执行任务。到这里,这不就像我们平常看到的开启一个线程执行任务的写法:new Thread(r).start()一样么,接下来我们进入Worker类看里面是怎么来的。
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;
this.thread = getThreadFactory().newThread(this);
}
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
该类只截取了部分代码,其他代码这里用不到,大家可以观看源码,这里我们只关心构造函数和run方法。在构造方法中可以看到创建了Thread对象,然后在addWorker方法中:
...
w = new Worker(firstTask);
final Thread t = w.thread;
...
if (workerAdded) {
t.start();
workerStarted = true;
}
...
调用了start方法,那么最终会通过底层创建线程然后执行run方法了。也就是这个方法:
/** Delegates main run loop to outer runWorker */
public void run() {
runWorker(this);
}
接着再进入runWorker方法探究探究:
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask; /* <1>首先获取提交的的任务*/
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
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();/* <2>最终会执行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);
}
}
到这里可以看到任务被提交后是如何到最后被执行的==》通过excute 一个Runnable任务,先进行判断当前线程数是否小于核心线程数,如果小于,则通过构建Worker对象获取Thread执行start方法,最终底层会执行Worker的run方法,到此就调用我们通过excute传进来的run方法了。
到这里我的事情还没完!我们已经通过测试一次excute很多任务过来,然后没有空闲线程执行的任务肯定要放入队列中等待被执行的。上面我们知道创建了Thread了,就算把任务执行完了,我们就不能这样让它结束了,肯定要冲队列中取任务继续执行的,这时我们还是看回runWorker(Worker w)方法。
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) { //这里可以看到一个while循环,也就是不断从队列中取任务的,主要看getTask()方法
.
.//省略了很多行
.
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);
}
}
private Runnable getTask() {
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) :
workQueue.take();//从这里三目运算符可以看出是从队列中取任务的。
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
到这里可以知道开启一个线程后进行不断的执行任务。
回到这里:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
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);
}
看reject方法:
final void reject(Runnable command) {
handler.rejectedExecution(command, this);
}
此时的handler就是我们一开始创建的ThreadPoolExecutor.AbortPolicy对象了,也可以自己自定义处理策略。
到此源码就解释就到这里,其中很多没有提及,因为涉及到很多细节,这里只从我们使用流程来探究源码。
如有错漏,欢迎指出!