CodeNote_1.0.2_CyclicBarrier を使用してスレッドをブロックし、マルチコア ソート機能を実装する

意味

ループ バリアは複数のスレッドと連携して、複数のスレッドがバリアの前で待機できるようにします。すべてのスレッドがこのバリアに到達するまで、後続のアクションを一緒に実行します。

コード

package JavaNote_101;

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

public class Java_102_CyclicBarrier {
    
    
    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
    
    
        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);
        final CyclicBarrier barrier = new CyclicBarrier(count+1);
        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 MyRunableC(barrier,array,start,end));
        }

        barrier.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 MyRunableC implements Runnable {
    
    
    private final CyclicBarrier barrier;
    private final int[] array;
    private final int start;
    private final int end;

    public MyRunableC(CyclicBarrier barrier, int[] array, int start, int end) {
    
    
        this.barrier = barrier;
        this.array = array;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
    
    
        Arrays.sort(array, start, end);
       try {
    
    
           barrier.await();
       }catch (InterruptedException e){
    
    

       }catch (BrokenBarrierException e){
    
    

       }
    }
}

おすすめ

転載: blog.csdn.net/h201601060805/article/details/130753797