Is it impossible to supply ForkJoinPool threads or a sample name?

I would like to set a name for the ForkJoinPool threads used by the work processing pool provided by

ExecutorService newWorkStealingPool(int parallelism)

or

ExecutorService newWorkStealingPool()

Until now, I could not find a way to set user names in the threads used by this ExecutorService, is there any way?

newWorkStealingPool()basically provides ForkJoinPool, but ForkJoinPoolalso does not have an open constructor with the specified name pattern.

Update : I found this constructor ForkJoinPoolthat accepts a factory thread ForkJoinPool.ForkJoinWorkerThreadFactory. But the factory should return ForkJoinWorkerThreadthat does not have a common constructor. Therefore, I assume that I will have to subclass ForkJoinWorkerThread.

+4
source share
2

, , 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); 
+10

( )

, ForkJoinPool s. "" , . java.util.concurrent.ForkJoinWorkerThread.InnocuousForkJoinWorkerThread, .

public class MyForkJoinThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
  @Override
  public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    return new NotSoInnocuousWorkerThread(pool);
  }
}

( , , , ....

public class NotSoInnocuousWorkerThread extends ForkJoinWorkerThread {
  protected NotSoInnocuousWorkerThread(ForkJoinPool pool) {
    super(pool);
  }
}

, :

System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory", 
                   MyForkJoinThreadFactory.class.getName());
+2

Source: https://habr.com/ru/post/1620234/


All Articles