-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSleepingTask.java
More file actions
50 lines (40 loc) · 1.35 KB
/
SleepingTask.java
File metadata and controls
50 lines (40 loc) · 1.35 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
50
package concurrency;
import java.io.*;
import java.util.concurrent.*;
import java.util.*;
/**
* RUN:
* javac concurrency/SleepingTask.java && java concurrency.SleepingTask
*
* OUTPUT:
* #0(9), #1(9), #2(9), #3(9), #4(9), #1(8), #3(8), #4(8), #0(8), #2(8), #0(7),
* #2(7), #3(7), #4(7), #1(7), #0(6), #1(6), #3(6), #4(6), #2(6), #1(5), #3(5),
* #2(5), #4(5), #0(5), #2(4), #3(4), #0(4), #4(4), #1(4), #4(3), #0(3), #2(3),
* #1(3), #3(3), #2(2), #0(2), #3(2), #1(2), #4(2), #3(1), #0(1), #1(1), #4(1),
* #2(1), #4(Liftoff!), #1(Liftoff!), #2(Liftoff!), #3(Liftoff!), #0(Liftoff!),
*/
public class SleepingTask extends LiftOff {
public void run() {
try {
while (countDown-- > 0) {
System.out.print(status());
// old style:
//
// Thread.sleep(100);
// new style:
//
TimeUnit.MILLISECONDS.sleep(100);
}
}
catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
exec.execute(new SleepingTask());
}
exec.shutdown();
}
}