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;
}
随手写的. 可能有点问题