aboutsummaryrefslogtreecommitdiff
path: root/challenge-142/lubos-kolouch/java/ch-2.java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-142/lubos-kolouch/java/ch-2.java')
-rw-r--r--challenge-142/lubos-kolouch/java/ch-2.java25
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-142/lubos-kolouch/java/ch-2.java b/challenge-142/lubos-kolouch/java/ch-2.java
new file mode 100644
index 0000000000..8823bd7f9d
--- /dev/null
+++ b/challenge-142/lubos-kolouch/java/ch-2.java
@@ -0,0 +1,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();
+ }
+}