线程池当一瞬间没有任务的时候,里面的线程会干啥
这个状态时候,放入一个线程,里面又会干啥
线程复用是啥
直接来核心源码
线程池内部类,线程执行时候会get任务,
这个里面
workQueue.take();
我们的que是阻塞,队列,当线程池里面没有任务的时候就会走这个里面
这个里面在看看
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
我们发现他会await,实际就是阻塞
在来回答第二个问题,当放入任务的时候,两个地方
1,execute里面,会根据线程数来决定是否创建线程
而这个时候,我们的线程仅仅是阻塞了而不是没有了
所以会
2,放入队列,
我们看看放入队列会干嘛
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items;
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
notEmpty.signal();
}
内部会调到signal,就是我们的唤醒线程
final boolean transferForSignal(Node node) {
/*
* If cannot change waitStatus, the node has been cancelled.
*/
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;
/*
* Splice onto queue and try to set waitStatus of predecessor to
* indicate that thread is (probably) waiting. If cancelled or
* attempt to set waitStatus fails, wake up to resync (in which
* case the waitStatus can be transiently and harmlessly wrong).
*/
Node p = enq(node);
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}
我们找到底层其实就是unpark,发出系统调用
我们知道,Thread仅仅是一个类,如何变成线程的呢
执行thread的start方法,其内部调用了很多的native方法,在操作系统层面开辟栈,分页表等待初始化虚拟内存的操作以及CPU资源分享的操作
这个是开销比较大的,其内存占用可能达到MB级别的,是重量级别的
而线程池本质是执行run方法,里面的线程会执行队列里面runable实现类的run方法,而不是start,执行一个再取一个,从而达到,线程栈,线程页表等内存以及CPU轮转时候的上下文切换,系统中断导致的问题。