我想为工作窃取池使用的ForkJoinPool的线程设置名称,由.提供
ExecutorService newWorkStealingPool(int parallelism)
要么
ExecutorService newWorkStealingPool()
到目前为止,我找不到在这个线程上设置自定义名称ExecutorService
的方法,有没有办法?
newWorkStealingPool()
基本上提供了一个ForkJoinPool
,但ForkJoinPool
也没有提供名称模式的公共构造函数.
更新:我现在发现这个构造函数
ForkJoinPool
需要一个线程工厂ForkJoinPool.ForkJoinWorkerThreadFactory
.但是工厂应该返回a ForkJoinWorkerThread
,它没有公共构造函数.所以我想我必须继承ForkJoinWorkerThread
.
这似乎是所需的最低代码,重用现有的默认工厂:
final ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory() { @Override public ForkJoinWorkerThread newThread(ForkJoinPool pool) { final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool); worker.setName("my-thread-prefix-name-" + worker.getPoolIndex()); return worker; } }; forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, false);