ForkJoinPool并行度=1死锁

发布于 2024-10-28 10:48:58 字数 2510 浏览 1 评论 0原文

我使用 jsr166y ForkJoinPool 在线程之间分配计算任务。但我显然一定做错了什么。

如果我创建具有并行性 > 的 ForkJoinPool,我的任务似乎可以完美地工作。 1(默认为 Runtime.availableProcessors();我一直在运行 2-8 个线程)。但如果我创建并行度 = 1 的 ForkJoinPool,我会在不可预测的迭代次数后看到死锁。

是的 - 设置parallelism = 1 是一种奇怪的做法。在本例中,我正在随着线程数的增加对并行算法进行分析,并且我想将单线程运行的并行版本与基线串行实现进行比较,以便准确地确定并行实现的开销。

下面是一个简单的示例,说明了我所看到的问题。 “任务”是对固定数组的虚拟迭代,递归地分为 16 个子任务。

如果以 THREADS = 2(或更多)运行,则它会可靠地运行完成,但如果以 THREADS = 1 运行,则总是会死锁。经过不可预测的迭代次数后,主循环挂在 ForkJoinPool.invoke() 中,等待 task.join(),然后工作线程退出。

我在 Linux 下运行 JDK 1.6.0_21 和 1.6.0_22,并使用几天前从 Doug Lea 网站下载的 jsr166y 版本 (http://gee.cs.oswego.edu/dl/concurrency-interest/index.html)

对于我所缺少的内容有什么建议吗?非常感谢。

package concurrent;

import jsr166y.ForkJoinPool;
import jsr166y.RecursiveAction;

public class TestFjDeadlock {

    private final static int[] intArray = new int[256 * 1024];
    private final static float[] floatArray = new float[256 * 1024];

    private final static int THREADS = 1;
    private final static int TASKS = 16;
    private final static int ITERATIONS = 10000;

    public static void main(String[] args) {

        // Initialize the array
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = i;
        }

        ForkJoinPool pool = new ForkJoinPool(THREADS);

        // Run through ITERATIONS loops, subdividing the iteration into TASKS F-J subtasks
        for (int i = 0; i < ITERATIONS; i++) {
            pool.invoke(new RecursiveIterate(0, intArray.length));
        }

        pool.shutdown();
    }

    private static class RecursiveIterate extends RecursiveAction {

        final int start;
        final int end;

        public RecursiveIterate(final int start, final int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        protected void compute() {

            if ((end - start) <= (intArray.length / TASKS)) {
                // We've reached the subdivision limit - iterate over the arrays
                for (int i = start; i < end; i += 3) {
                    floatArray[i] += i + intArray[i];
                }

            } else {
                // Subdivide and start new tasks
                final int mid = (start + end) >>> 1;
                invokeAll(new RecursiveIterate(start, mid), new RecursiveIterate(mid, end));
            }
        }
    }
}

I'm using the jsr166y ForkJoinPool to distribute computational tasks amongst threads. But I clearly must be doing something wrong.

My tasks seem to work flawlessly if I create the ForkJoinPool with parallelism > 1 (the default is Runtime.availableProcessors(); I've been running with 2-8 threads). But if I create the ForkJoinPool with parallelism = 1, I see deadlocks after an unpredictable number of iterations.

Yes - setting parallelism = 1 is a strange practice. In this case, I'm profiling a parallel algorithm as thread-count increases, and I want to compare the parallel version, run with to a single thread, to a baseline serial implementation, so as to accurately ascertain the overhead of the parallel implementation.

Below is a simple example that illustrates the issue I'm seeing. The 'task' is a dummy iteration over a fixed array, divided recursively into 16 subtasks.

If run with THREADS = 2 (or more), it runs reliably to completion, but if run with THREADS = 1, it invariably deadlocks. After an unpredictable number of iterations, the main loop hangs in ForkJoinPool.invoke(), waiting on task.join(), and the worker thread exits.

I'm running with JDK 1.6.0_21 and 1.6.0_22 under Linux, and using a version of jsr166y downloaded a few days ago from Doug Lea's website (http://gee.cs.oswego.edu/dl/concurrency-interest/index.html)

Any suggestions for what I'm missing? Many thanks in advance.

package concurrent;

import jsr166y.ForkJoinPool;
import jsr166y.RecursiveAction;

public class TestFjDeadlock {

    private final static int[] intArray = new int[256 * 1024];
    private final static float[] floatArray = new float[256 * 1024];

    private final static int THREADS = 1;
    private final static int TASKS = 16;
    private final static int ITERATIONS = 10000;

    public static void main(String[] args) {

        // Initialize the array
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = i;
        }

        ForkJoinPool pool = new ForkJoinPool(THREADS);

        // Run through ITERATIONS loops, subdividing the iteration into TASKS F-J subtasks
        for (int i = 0; i < ITERATIONS; i++) {
            pool.invoke(new RecursiveIterate(0, intArray.length));
        }

        pool.shutdown();
    }

    private static class RecursiveIterate extends RecursiveAction {

        final int start;
        final int end;

        public RecursiveIterate(final int start, final int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        protected void compute() {

            if ((end - start) <= (intArray.length / TASKS)) {
                // We've reached the subdivision limit - iterate over the arrays
                for (int i = start; i < end; i += 3) {
                    floatArray[i] += i + intArray[i];
                }

            } else {
                // Subdivide and start new tasks
                final int mid = (start + end) >>> 1;
                invokeAll(new RecursiveIterate(start, mid), new RecursiveIterate(mid, end));
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2024-11-04 10:48:58

看起来像是 ForkJoinPool 中的错误。我在类的用法中看到的所有内容都适合您的示例。唯一的其他可能性可能是您的任务之一抛出异常并异常死亡(尽管仍然应该处理)。

looks like a bug in the ForkJoinPool. everything i can see in the usage for the class fits your example. the only other possibility might be one of your tasks throwing an exception and dying abnormally (although that should still be handled).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文