简体   繁体   English

并发线程合并排序

[英]concurrent Threads Merge Sort

I have to code a concurrent Merge Sort Aplication.我必须编写一个并发的合并排序应用程序。 Every time the array gets split up, I have to create a new thread for the right half (max. Number of Threads is 5 -> so 5 Times) which continues the Mergesort algorithm.每次数组被拆分时,我都必须为右半部分创建一个新线程(最大线程数为 5 -> 所以是 5 次),它继续 Mergesort 算法。

在此处输入图片说明

That's my program:那是我的程序:

class Mergesorts implements Runnable{

    private int[] internal;

    Mergesorts(int[] arr) {
        internal = arr;
    }

    private void processCommand(int [] array) {
        if (array.length > 1) { 
            int[] left = leftHalf(array);
            int[] right = rightHalf(array);
            processCommand(left);
            processCommand(right);
            merge(array, left, right);
        }
    }

    public int[] rightHalf(int[] array) {
        int size1 = array.length / 2;
        int size2 = array.length - size1;
        int[] right = new int[size2];
        for (int i = 0; i < size2; i++) {
            right[i] = array[i + size1];
        }
        return right;
    }

    public void run() {
        processCommand(internal);
    }
}

How can I rewrite my code to sort concurrently as described above?如何重写我的代码以如上所述同时排序?

How can i edit my code so it only creates up to 5 Threads and not more?如何编辑我的代码,使其最多只能创建 5 个线程而不是更多?

private void processCommand(int [] array) {
    if (array.length > 1) {


        int[] right = rightHalf(array);
        int[] left = leftHalf(array);


        Mergesorts worker2 = new Mergesorts(right);
        Thread s = new Thread(worker2);
        s.start();

        processCommand(left);

        try {
            s.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        merge(array, left, right);}

}

Start the new thread in the processCommand() method, not in the rightHalf() method.在 processCommand() 方法中启动新线程,而不是在 rightHalf() 方法中。 If you have threads available, then instead of the line calling processCommand(right), construct a new Mergesorts object on right and start a thread with it.如果有可用线程,然后代替排队叫号processCommand(右),构建一个新的Mergesorts对象right ,并启动一个线程它。 Don't forget to call join() on the thread to ensure it completes before doing the merge.不要忘记在线程上调用 join() 以确保它在进行合并之前完成。

To improve parallelism, you can start the new thread on the right half of the array before processing the left half of the array, so both are done at the same time - that is, move processCommand(left) to after the block that either does processCommand(right) or starts a new thread.为了提高并行性,您可以在处理数组的左半部分之前在数组的右半部分启动新线程,因此两者是同时完成的 - 也就是说,将 processCommand(left) 移动到执行此操作的块之后processCommand(right) 或启动一个新线程。 The join() call should be after both halves are done, but before the merge. join() 调用应该在两半完成之后,但在合并之前。

Edit: you are getting closer;编辑:你越来越近了; to address your comment, you need something like this:要解决您的评论,您需要这样的东西:

private void processCommand(int [] array) {
    if (array.length <= 1)
        {return;}

    int[] left = leftHalf(array);
    int[] right = rightHalf(array);
    Thread rightThread = null;

    if (b <= 5) {
        ++b;
        Mergesorts worker = new Mergesorts(right);
        rightThread = new Thread(worker);
        rightThread.start();
    } else {
        processCommand(right);
    }

    processCommand(left);

    if (null != rightThread) {
        try {
            rightThread.join();
            b--
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // better error handling would be good
        }
    }

    merge(array, left, right);
}

b also needs to be volatile to ensure the different threads can see how many threads are currently executing. b还需要 volatile 以确保不同的线程可以看到当前正在执行的线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM