java读取keepalive信息_JAVA分析线程池中的keepAliveTime
参数具体实现
分析线程池中的keepAliveTime参数具体实现
参数意义
创建线程池时,有⼀个重要参数就是keepAliveTime,标记线程空闲多久后被释放。
那么他到底是怎么实现的呢?
猜想
有⼀个线程在维护时间,可笑。专门有⼀个线程去维护,浪费资源,⽽且时间也不够精确。⽽且还要开辟空间记录线程开始空闲的时间,消耗空间。
源码分析
urrent.ThreadPoolExecutor#getTask
/**
* Performs blocking or timed wait for a task, depending on
* current configuration settings, or returns null if this worker
* must exit because of any of:
* 1. There are more than maximumPoolSize workers (due to
* a call to setMaximumPoolSize).
* 2. The pool is stopped.
* 3. The pool is shutdown and the queue is empty.
* 4. This worker timed out waiting for a task, and timed-out
* workers are subject to termination (that is,
* {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
* both before and after the timed wait, and if the queue is
* non-empty, this worker is not the last thread in the pool.
*
* @return task, or null if the worker must exit, in which case
* workerCount is decremented
*/
private Runnable getTask() {
boolean timedOut = false; // Did the last poll() time out?
for (;;) {
int c = ();
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 {
// 这⼀步是关键,需要了解poll和take的区别,take会进⾏阻塞。
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}
poll ⽅法作⽤是移除并返回队列的头节点。但是如果当队列⾥⾯是空的,没有任何东西可以移除的时候,便会返回 null 作为提⽰。
带时间参数的 poll ⽅法:如果能够移除,便会⽴刻返回这个节点的内容;如果队列是空的就会进⾏等待,等待时间正是我们指定的时间,直到超时时间到了,如果队列⾥依然没有元素可供移除,便会返回 null 作为提⽰。
take ⽅法的作⽤是获取并移除队列的头结点。通常在队列⾥有数据的时候会正常取出数据并删除;但是如果执⾏ take 的时候队列⾥⽆数据,则阻塞,直到队列⾥有数据;⼀旦队列⾥有数据了,就会⽴刻解除阻塞状态,并且取到数据。
timed为true则标志着:允许核⼼线程超时被释放或者当前线程数超过核⼼线程数。⼀旦为tue,就会去使⽤阻塞对垒的poll⽅法,如果keepAliveTime的时间⾥获取不到任务,就会返回Null,在上⼀级,也就是runWorker⽅法中去释放资源。
urrent.ThreadPoolExecutor#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循环获取任务,如果获取不到,就跳出循环,在finally中进⾏释放。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 (((), STOP) ||
(Thread.interrupted() &&
(), 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;
wpletedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
/
/ 去释放当前的worker w。
processWorkerExit(w, completedAbruptly);
}
}
java线程池创建的四种阻塞队列的poll⽅法⼜是怎么实现超时的呢?
但是我们较真来看,打破砂锅问到底,阻塞队列⼜是如何做到去完成超时的呢?以urrent.DelayQueue为例
源码分析
urrent.DelayQueue#poll(long, urrent.TimeUnit) /**
* Retrieves and removes the head of this queue, waiting if necessary
* until an element with an expired delay is available on this queue,
* or the specified wait time expires.
*
* @return the head of this queue, or {@code null} if the
* specified waiting time elapses before an element with
* an expired delay becomes available
* @throws InterruptedException {@inheritDoc}
*/
public E poll(long timeout, TimeUnit unit) throws InterruptedException { long nanos = Nanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
E first = q.peek();
if (first == null) {
if (nanos <= 0)
// 已经超时就返回null
return null;
else
// 未超时就去await
nanos = available.awaitNanos(nanos);
} else {
long delay = Delay(NANOSECONDS); if (delay <= 0)
return q.poll();
if (nanos <= 0)
return null;
first = null; // don't retain ref while waiting
if (nanos < delay || leader != null)
// 未超时就去await
nanos = available.awaitNanos(nanos);
else {
Thread thisThread = Thread.currentThread(); leader = thisThread;
try {
// 未超时就去await
long timeLeft = available.awaitNanos(delay); nanos -= delay - timeLeft;
} finally {
if (leader == thisThread)
leader = null;
}
}
}
}
} finally {
if (leader == null && q.peek() != null) available.signal();
lock.unlock();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。