19

I would like to set name for threads of the ForkJoinPool used by work stealing pool, supplied by

ExecutorService newWorkStealingPool(int parallelism)

or

ExecutorService newWorkStealingPool()

So far I could not find a way to set custom names on threads used by this ExecutorService, is there a way?

newWorkStealingPool() basically supplies a ForkJoinPool, but ForkJoinPool also doesn't have a public constructor with supplied name pattern.

update: I have now found this constructor of ForkJoinPool 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.

2
  • Isn't that just a convenience method? You can make your own ForkJoinPool with a thread factory that does the naming for you.
    – pvg
    Dec 16, 2015 at 2:53
  • Oops, don't know but somehow missed this. docs.oracle.com/javase/8/docs/api/java/util/concurrent/… Yup, as you said that was my thought too but failed to notice mentioned constructor.
    – cpz
    Dec 16, 2015 at 2:59

3 Answers 3

45

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); 
4
  • 1
    Looks good except for the fact that Executors.newWorkStealingPool() sets the last argument (async) to true. You should probably do the same.
    – Gili
    Feb 6, 2018 at 20:24
  • Nice catch. In general, the documentation for the async parameter is: > if true, establishes local first-in-first-out scheduling mode for forked tasks > that are never joined. This mode may be more appropriate than default > locally stack-based mode in applications in which worker threads only > process event-style asynchronous tasks. For default value, use false. Feb 9, 2018 at 7:46
  • 4
    I tried an example, and worker.getPoolIndex() always returns zero.
    – k.liakos
    Jan 6, 2022 at 8:18
  • 1
    @k.liakos you are correct. I don't think you can invoke worker.getPoolIndex() before the pool has started. In any case, you could create a static AtomicLong field and generate IDs using AtomicLong.getAndIncrement().
    – Gili
    Jun 22, 2022 at 3:54
5

(Answering your update)

The following should allow you full control over the threads produced by your ForkJoinPools. In my case I wanted to be able to do "dangerous" stuff like access system properties. The default implementation uses java.util.concurrent.ForkJoinWorkerThread.InnocuousForkJoinWorkerThread which has a security manager and zero permissions.

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

and the worker thread (that now has all the same permissions as the rest of your application is this like this, plus whatever else you wanted....

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

And you need to either pass the following property or set it in your code like this:

System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory", 
                   MyForkJoinThreadFactory.class.getName());
1
  • I had already updated the question, method suggested is same as what I had done in the end. thanks
    – cpz
    Jun 9, 2016 at 7:13
3

Here is @Morten Haraldsen's answer ported to Kotlin:

val forkJoinPool =
    ForkJoinPool.ForkJoinWorkerThreadFactory { pool ->
        ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool).apply {
            name = "my-thread-prefix-name-$poolIndex"
        }
    }.let { factory -> ForkJoinPool(Runtime.getRuntime().availableProcessors(), factory, null, true) }

7
  • 1
    the OP mentioned Java, not Kotlin.
    – mr.M
    Oct 21, 2020 at 9:56
  • 1
    Thanks for the down vote explanation. When searching for how to do this in Kotlin this question is near the top of the results so this answer still seems valuable to me.
    – Matt Klein
    Oct 21, 2020 at 15:31
  • 1
    And the OP does not mention Java, they ask about ForkJoinPool which is a JVM class usable from Java, Kotlin, Scala, etc.
    – Matt Klein
    Oct 21, 2020 at 15:36
  • 1
    there is the Java tag for this question
    – mr.M
    Oct 22, 2020 at 7:28
  • 2
    @mr.M I think you meant Clojure. Oct 29, 2020 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.