Java – how to configure a single threaded forkjoinpool?

Can forkjoinpool be configured to use 1 execution thread?

I am executing code that calls Random in ForkJoinPool. Every time I run, I encounter different runtime behavior, so it's difficult to investigate regression

I want the code base to provide "debug" and "release" modes The "debug" mode configures random with a fixed seed and forkjoinpool with a single execution thread The "release" mode will use the random seed provided by the system and the default number of forkjoinpool threads

I tried to configure forkjoinpool with 1 parallelism, but it uses 2 threads (the main thread and the second worker thread) Any ideas?

Solution

So, it turned out that I was wrong

When forkjoinpool is configured with parallelism set to 1, only one thread executes the task ForkJoin. Main thread blocked on get() It does not actually perform any tasks

In other words, it turns out that providing deterministic behavior is really tricky Here are some of the problems I must correct:

>If the worker thread is idle long enough, forkjoinpool is using different worker threads (with different names) to perform tasks For example, if the main thread hangs at a debug breakpoint, the worker thread becomes idle and closes When I resume execution, forkjointhread will start a new worker thread with a different name To solve this problem, I have to use provide a custom forkjoinworkerthreadfactory implementation that returns NULL if the forkjoinpool already has a live worker Even if the worker thread closes and returns again, I make sure my code returns the same random instance. > Sets with non deterministic iterative order (such as HashMap or HashSet) cause elements to obtain random numbers in different order each time they run I corrected this problem by using LinkedHashMap and linkedhashset. > An object with a non deterministic hashcode () implementation, such as enum hashCode(). I forgot what the problem was, but I corrected it by calculating hashcode () myself instead of relying on built-in methods

This is an example implementation of forkjoinworkerthreadfactory:

class MyForkJoinWorkerThread extends ForkJoinWorkerThread
{
    MyForkJoinWorkerThread(ForkJoinPool pool)
    {
        super(pool);
        // Change thread name after ForkJoinPool.registerWorker() does the same
        setName("DETERMINISTIC_WORKER");
    }
}

ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory()
{
    private WeakReference<Thread> currentWorker = new WeakReference<>(null);

    @Override
    public synchronized ForkJoinWorkerThread newThread(ForkJoinPool pool)
    {
        // If the pool already has a live thread,wait for it to shut down.
        Thread thread = currentWorker.get();
        if (thread != null && thread.isAlive())
        {
            try
            {
                thread.join();
            }
            catch (InterruptedException e)
            {
                log.error("",e);
            }
        }
        ForkJoinWorkerThread result = new MyForkJoinWorkerThread(pool);
        currentWorker = new WeakReference<>(result);
        return result;
    }
};
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>