1.调用线程池的execute方法
public void execute(Runnable command) {
......
addWorker(command, true)
......
}
2.addWorker方法
private boolean addWorker(Runnable firstTask, boolean core) {
......
w = new Worker(firstTask);
final Thread t = w.thread;
......
t.start();
......
}
3.线程池内部类-Worker,从线程工厂对象中获取一个线程,传入当前任务对象引用
private final class Worker
extends AbstractQueuedSynchronizer
implements Runnable
Worker(Runnable firstTask) {
setState(-1); // inhibit interrupts until runWorker
this.firstTask = firstTask;
this.thread = getThreadFactory().newThread(this);
}
4. addWork()方法内部调用 t.start()后,执行Worker对象的run方法
public void run() {
runWorker(this);
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
......
task.run();
......
}