-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDaemonFromFactory.java
More file actions
42 lines (34 loc) · 993 Bytes
/
DaemonFromFactory.java
File metadata and controls
42 lines (34 loc) · 993 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
30
31
32
33
34
35
36
37
38
39
40
41
42
package concurrency;
import java.io.*;
import java.util.concurrent.*;
import net.mindview.util.*;
/**
* RUN:
* javac concurrency/DaemonFromFactory.java && java concurrency.DaemonFromFactory
*
* OUTPUT:
*
*/
public class DaemonFromFactory implements Runnable {
public void run() {
try {
while (true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(Thread.currentThread() + " " + this);
}
}
catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool(
new DaemonThreadFactory()
);
for (int i = 0; i < 10; i++) {
exec.execute(new DaemonFromFactory());
}
System.out.println("All daemons are running");
TimeUnit.MILLISECONDS.sleep(500);
}
}