CodeNote_1.0.1_使用CountDownLatch对多核机器上的数据进行排序

方法

CountDownLatch方法可以等待所有的线程全部countDown,执行完变成0,然后再执行其他程序。

代码

package JavaNote_101;
import java.util.Arrays;
import java.util.concurrent.*;

public class Java_101_CountDownLatchV2 {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        int count = 4;
        int[] array = {
    
    24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        int n = array.length;

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4,4,60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(count));

//        ExecutorService executor = Executors.newFixedThreadPool(processors);
        CountDownLatch latch = new CountDownLatch(count);

        int batchSize = n / count; // 使用向上取整来确保最后一个子数组能够处理剩余的元素
        for (int i = 0; i < count; i++) {
    
    
            int start = i*batchSize;
            int end = (i+1)*batchSize;
            if (i == count-1) end = n;
            threadPoolExecutor.execute(new MyRunable(latch,array,start,end));
        }

        latch.await();

        threadPoolExecutor.shutdown();
        //然后再对所有的排序后的数据进行合并
        mergeSortedSubArrays(array, batchSize);
        for (int num : array) {
    
    
            System.out.print(num + " ");
        }
    }
    private static void mergeSortedSubArrays(int[] array, int batchSize) {
    
    
        int n = array.length;
        int[] temp = new int[n];
        int startIndex = 0;

        while (batchSize < n) {
    
    
            int i = 0;
            while (i + 2 * batchSize < n) {
    
    
                merge(array, temp, i, i + batchSize, i + 2 * batchSize);
                i += 2 * batchSize;
            }
            if (i + batchSize < n) {
    
    
                merge(array, temp, i, i + batchSize, n);
            } else {
    
    
                System.arraycopy(array, i, temp, i, n - i);
            }
            System.arraycopy(temp, 0, array, 0, n);

            batchSize *= 2;
        }
    }

    private static void merge(int[] array, int[] temp, int startIndex, int midIndex, int endIndex) {
    
    
        int i = startIndex;
        int j = midIndex;
        int k = startIndex;

        while (i < midIndex && j < endIndex) {
    
    
            if (array[i] <= array[j]) {
    
    
                temp[k++] = array[i++];
            } else {
    
    
                temp[k++] = array[j++];
            }
        }

        while (i < midIndex) {
    
    
            temp[k++] = array[i++];
        }

        while (j < endIndex) {
    
    
            temp[k++] = array[j++];
        }
    }
}
class MyRunable implements Runnable {
    
    
    private final CountDownLatch latch;
    private final int[] array;
    private final int start;
    private final int end;

    public MyRunable(CountDownLatch latch, int[] array, int start, int end) {
    
    
        this.latch = latch;
        this.array = array;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
    
    
        Arrays.sort(array, start, end);
        latch.countDown();
    }
}

猜你喜欢

转载自blog.csdn.net/h201601060805/article/details/130753560