0
  • 随手写的. 可能有点问题

    public Byte[] segmentSort(Byte[] bytes, int step, int threadNum) throws InterruptedException {
        final int len = bytes.length;
        Byte[] res = new Byte[len];
        final AtomicInteger cur = new AtomicInteger(0);
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        IntStream.range(0, threadNum).forEach(i -> {
            final Thread thread = new Thread(() -> {
                int left = 0;
                int right = 0;
                while (left != len) {       //[left,right)
                    do {
                        left = cur.get();
                        right = Math.min(left + step, len);
                    } while (!cur.compareAndSet(left, right));
                    if (left == right) break;
                    Arrays.sort(bytes, left, right, ((o1, o2) -> o2 - o1));
                    System.arraycopy(bytes, left, res, left, right - left);
                }
                countDownLatch.countDown();
            });
            thread.setName("segment_thread_" + i);
            thread.setDaemon(true);
            thread.start();
        });
        countDownLatch.await();
        return res;
    }
    
    1285202635850326016  评论     打赏       coder_tbs
    相关问题
    资质平平 · Android
    2025-02-20 09:14 92 100
    尖沙咀-段坤 · 项目
    2025-01-06 23:39 18 2
    雅澤yaduo · 领券联盟
    2025-01-03 20:39 37 50
    呆瓜小董 · webview
    2024-11-30 18:02 68 20
    阿肥 · 鸿蒙next
    2024-10-25 18:07 35 100
    尖沙咀-段坤 · 安卓
    2024-09-11 11:03 31 2
    YanLQ · AOSP
    2024-08-10 11:57 35 100
    断点 · vue
    2024-08-08 10:05 79 30
    幻影~ · 安卓 / 面试
    2024-06-15 18:51 48 2