aboutsummaryrefslogtreecommitdiff
path: root/challenge-142/lubos-kolouch/java/ch-2.java
blob: 8823bd7f9d003ab8cabcc56f5ab7ddc72b5215a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.concurrent.*;

public class SleepSort {
  public static void main(String[] args) throws InterruptedException {
    int[] nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    ExecutorService executor = Executors.newCachedThreadPool();

    CountDownLatch latch = new CountDownLatch(nums.length);
    for (final int num : nums) {
      executor.execute(() -> {
        try {
          TimeUnit.SECONDS.sleep(num);
          System.out.print(num + " ");
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
        } finally {
          latch.countDown();
        }
      });
    }

    latch.await();
    executor.shutdown();
  }
}