all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,31 @@
|
|||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class SleepSort {
|
||||
public static void sleepSortAndPrint(int[] nums) {
|
||||
final CountDownLatch doneSignal = new CountDownLatch(nums.length);
|
||||
for (final int num : nums) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
doneSignal.countDown();
|
||||
try {
|
||||
doneSignal.await();
|
||||
|
||||
//using straight milliseconds produces unpredictable
|
||||
//results with small numbers
|
||||
//using 1000 here gives a nifty demonstration
|
||||
Thread.sleep(num * 1000);
|
||||
System.out.println(num);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int[] nums = new int[args.length];
|
||||
for (int i = 0; i < args.length; i++)
|
||||
nums[i] = Integer.parseInt(args[i]);
|
||||
sleepSortAndPrint(nums);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue