-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTh09LogEvents.java
More file actions
57 lines (48 loc) · 1.75 KB
/
Th09LogEvents.java
File metadata and controls
57 lines (48 loc) · 1.75 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
51
52
53
54
55
56
57
public class Th09LogEvents {
private static final int APP_THREADS = 3;
private static final int MESSAGES_PER_THREAD = 5;
public static void main(String[] args) {
Th09Logger logger = new Th09Logger();
Thread loggerThread = new Thread(logger);
loggerThread.setDaemon(true);
loggerThread.start();
Thread[] appThreads = new Thread[APP_THREADS];
for (int i = 0; i < APP_THREADS; i++) {
Thread t = new Thread(new AppAction(logger, i));
t.start();
appThreads[i] = t;
}
try {
for (int i = 0, l = appThreads.length; i < l; i++) {
appThreads[i].join();
}
System.out.println("Production finished");
Thread.sleep(1000);
System.out.println("Waited enough - interrupt the consumer");
loggerThread.interrupt();
System.exit(0);
}
catch(InterruptedException ie) {}
}
private static final class AppAction implements Runnable {
private final Th09Logger logger;
private final int index;
AppAction(Th09Logger logger, int index) {
this.logger = logger;
this.index = index;
}
public void run() {
String name = Thread.currentThread().getName();
for (int i = 0; i < MESSAGES_PER_THREAD; i++) {
logger.log("App thread " + name + " message #" + (i + 1));
try {
Thread.sleep(sleepAmount(index, i));
}
catch(InterruptedException ie) {}
}
}
}
private static final int sleepAmount(int index, int msg) {
return ((index) * 10) + ((msg) * 10);
}
}