forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleThreadExecutor2.java
More file actions
29 lines (28 loc) · 973 Bytes
/
SingleThreadExecutor2.java
File metadata and controls
29 lines (28 loc) · 973 Bytes
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
// threads/SingleThreadExecutor2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.concurrent.*;
public class SingleThreadExecutor2 {
public static void main(String[] args)
throws InterruptedException {
ExecutorService exec =
Executors.newSingleThreadExecutor();
for(int id = 0; id < 10; id++)
exec.execute(new SleepAndPrintTask(id));
exec.shutdown();
exec.awaitTermination(5, TimeUnit.SECONDS);
}
}
/* Output:
SleepAndPrintTask[0] pool-1-thread-1
SleepAndPrintTask[1] pool-1-thread-1
SleepAndPrintTask[2] pool-1-thread-1
SleepAndPrintTask[3] pool-1-thread-1
SleepAndPrintTask[4] pool-1-thread-1
SleepAndPrintTask[5] pool-1-thread-1
SleepAndPrintTask[6] pool-1-thread-1
SleepAndPrintTask[7] pool-1-thread-1
SleepAndPrintTask[8] pool-1-thread-1
SleepAndPrintTask[9] pool-1-thread-1
*/