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
    相关问题
    康师傅的小跟班 · JAVA
    2019-09-10 05:09 1295 2
    2020-03-08 05:30 845 2
    不易 · java
    2020-06-06 19:41 461 2
    夙夜星辰叹 · javaweb
    2020-06-24 03:07 457 2
    2020-07-21 06:26 937 2
    CodeWwang · JAVA
    2020-07-22 06:11 440 2
    路不离开 · JAVA / IDEA
    2020-09-02 20:59 395 2