记录线程使用后,所感所想
一般来说 , jdk提供的创建线程池有如下三种方式 , 其特点也在截图里标注了
但通过查阅阿里巴巴相关手册 , 对实际开发中线程池的使用有如下规范:
为什么不用Excutor?
有缺陷的:
①:FixedThreadPool和SingleThreadPool,允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM。
②:CachedThreadPool和ScheduledThreadPool,允许的创建线程数量为Integer.MAX_VALUE,可能会创建大量的线程,从而导致OOM。
ThreadPoolExecutor的构造方法介绍(具体可以传哪些参数)
构造方法说明 |
---|
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) 用给定的初始参数和默认的线程工厂及被拒绝的执行处理程序创建新的 ThreadPoolExecutor。 |
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) 用给定的初始参数和默认的线程工厂创建新的 ThreadPoolExecutor。 |
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) 用给定的初始参数和默认被拒绝的执行处理程序创建新的 ThreadPoolExecutor。 |
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) 用给定的初始参数创建新的 ThreadPoolExecutor。 |
执行过程:
通过一段打印日志来观察
执行第1次
i:1, queueSize:0, poolSize:1, coreSize:2, maxSize:3
执行第2次
i:2, queueSize:0, poolSize:2, coreSize:2, maxSize:3
执行第3次
i:3, queueSize:1, poolSize:2, coreSize:2, maxSize:3
执行第4次
i:4, queueSize:2, poolSize:2, coreSize:2, maxSize:3
执行第5次
i:5, queueSize:3, poolSize:2, coreSize:2, maxSize:3
执行第6次
i:6, queueSize:3, poolSize:3, coreSize:2, maxSize:3
执行第7次
Exception in thread "main" java.util.concurrent.RejectedExecutionException
1.当添加第一个任务的时候,由于线程池空着,直接创建核心线程来处理请求;
2.当已经添加完两个请求,添加第三个请求的时候,核心线程数已满,则往队列里面添加,此时queueSize=1;
3.添加3、4、5任务,都被添加到了队列里面,此时queueSize=3;
4.添加6任务的时候,核心线程已满,队列已满,运行的线程数小于maximumPoolSize,那么线程池再处理一个任务,此时poolSize=3,线程池的任务满了;
5.添加7任务的时候,由于线程池里面的任务还没有执行完,而队列也是满的,线程池处理不了这么多任务了,抛出异常java.util.concurrent.RejectedExecutionException。
综上:先创建核心线程,够数后往队列里面塞,塞满继续创建执行线程,再满后抛出拒绝执行的异常。我们在执行的时候希望往线程池里面一直扔,盛不下了也别抛异常,怎么办?
第一种方法
可以判断池子大小, 若满了,则阻塞 , 不往里面扔:
(threadPoolExecutor.getPoolSize()+linkedBlockingQueue.size())==(queueCapacity + maximumPoolSize),表示当前队列里面的线程数加上线程池里面当前线程数等于当前线程池可处理的最大线程的时候,进行Thread.sleep(1000L)等待,知道线程池有空闲资源的时候继续执行添加操作。
第二种方法
使用阻塞队列ArrayBlockingQueue , 其阻塞机制也相当于实现了Thread.sleep()的效果。
如何从队列里面获取线程?
这个就要查找源码了,线程池执行线程调用方法是ThreadPoolExecutor.execute(Runnable command)。
查看execute的源码:
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);
}
其中有个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);
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();
workerStarted = true;
}
}
} finally {
if (! workerStarted)
addWorkerFailed(w);
}
return workerStarted;
}
可以看到,传进的参数firstTask作为一个参数构造Worker后,又从Worker对象中找到取了了线程去执行。
继续看下Worker的构造方法:
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
Worker的定义,继承了Runnable,也就是说它本身就是个线程。,说明它是可被线程驱动执行的任务。
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
构造方法里,thread变量构造的线程是它本身。
也就是说,最终执行的是Worker类的run方法。
看run方法:
public void run() {
runWorker(this);
}
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) {
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);
}
}
我看到,其中有一个执行:task = getTask()
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;
}
}
}
这个方法就是在从队列里面取线程:workQueue.tack()。这个方法是阻塞的,如果取不到会一直等着,知道直到队列里面放入了新的线程。