-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleepSort.java
More file actions
49 lines (38 loc) · 1.21 KB
/
Copy pathSleepSort.java
File metadata and controls
49 lines (38 loc) · 1.21 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package MyPractice;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class SleepSort {
static int [] a = {3,90,5,1,2,4,30,5,6,7,8};
static int i=0;
public static void main(String [] args) throws Exception
{
ExecutorService es = Executors.newFixedThreadPool(a.length);
//AtomicInteger index = new AtomicInteger(0);
int [] b = new int [a.length];
for(int i=0;i<a.length;i++)
{
final int j=i;
es.execute(()->{
try {
TimeUnit.SECONDS.sleep(a[j]);
System.out.println("Woke and printing " + a[j]);
b[getAndIncrementIndex()]=a[j];
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
es.shutdown();
es.awaitTermination(3, TimeUnit.MINUTES);
for(int i =0;i<b.length;i++)
{
System.out.println(b[i]);
}
}
public static synchronized int getAndIncrementIndex()
{
return i++;
}
}