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 放进线程里跑起来。
本文深入探讨了ThreadPerTaskExecutor类的实现原理,这是一种特殊的Executor,用于为每个任务创建独立的线程。文章详细介绍了其构造函数和execute方法,解释了如何通过线程工厂为线程命名,并将Runnable任务放入线程中执行。
593

被折叠的 条评论
为什么被折叠?



