问题描述
我想为工作窃取池使用的ForkJoinPool的线程设置名称,由
I would like to set name for threads of the ForkJoinPool used by work stealing pool, supplied by
ExecutorService newWorkStealingPool(int parallelism)
或
ExecutorService newWorkStealingPool()
到目前为止,我找不到设置自定义的方法这个 ExecutorService
使用的线程上的名称有没有办法?
So far I could not find a way to set custom names on threads used by this ExecutorService
, is there a way?
newWorkStealingPool()
基本上提供 ForkJoinPool
,但 ForkJoinPool
也没有提供的公共构造函数名称模式。
newWorkStealingPool()
basically supplies a ForkJoinPool
, but ForkJoinPool
also doesn't have a public constructor with supplied name pattern.
更新:
我现在发现
的构造函数需要一个线程工厂 ForkJoinPool.ForkJoinWorkerThreadFactory
。但是工厂应该返回 ForkJoinWorkerThread
,它没有公共构造函数。所以我想我必须继承 ForkJoinWorkerThread
。
update:I have now found this constructor ofForkJoinPool
which takes a thread factory ForkJoinPool.ForkJoinWorkerThreadFactory
. But factory should return a ForkJoinWorkerThread
, which doesn't have a public constructor. So I guess I will have to subclass ForkJoinWorkerThread
.
推荐答案
这似乎作为最低要求的代码,重用现有的默认工厂:
This seems to be the minimum required code, reusing the existing default factory:
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);
这篇关于是不是可以为ForkJoinPool提供线程库或名称模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!