Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
57 lines (48 loc) · 1.75 KB

File metadata and controls

57 lines (48 loc) · 1.75 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.