public final class ThreadPerTaskExecutor implements Executor {
private final ThreadFactory threadFactory
;
public ThreadPerTaskExecutor(ThreadFactory threadFactory
) {
if (threadFactory
== null
) {
throw new NullPointerException("threadFactory");
}
this.threadFactory
= threadFactory
;
}
@Override
public void execute(Runnable command
) {
threadFactory
.newThread(command
).start();
}
}
Executor 的 execute 方法往往是对入参 Runnable 进行操作,这里是把任务 Runnable 放进线程里跑起来。