搞笑的排序算法-睡觉排序

66次阅读

共计 917 个字符,预计需要花费 3 分钟才能阅读完成。

搞笑的排序算法 — 睡觉排序
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

/**
* CopyRight (c) 2018 freebug.org Technology Inc.
*
* @author jimmiesong@yeah.net
* @date 2018/11/20 11:17
* @since 1.8
*/
public class SleepSort {
public static void main(String[] args) {
// int[] array = new int[]{5446, 584, 1, 4, 3, 777, 8, 9, 33, 6666, 33};

int[] array = getArray();
CountDownLatch latch = new CountDownLatch(array.length);

System.out.println(“Array: ” + Arrays.toString(array));

for (int i = 0; i < array.length; i++) {
final int value = array[i];

new Thread(() -> {
latch.countDown();

try {
Thread.sleep(value);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.print(value + “\n”);
}).start();
}

try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static int[] getArray() {
int loop = (int) (Math.random() * 20);
if (loop <= 0) {
return getArray();
}

int[] array = new int[loop];
for (int i = 0; i < loop; i++) {
array[i] = (int) (Math.random() * 10000);
}

return array;
}
}

以上代码,看得懂的人早已捧腹大笑,看不懂的人,肯定在嗤之以鼻!

正文完
 0