-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadTest.java
More file actions
121 lines (104 loc) · 3.67 KB
/
ThreadTest.java
File metadata and controls
121 lines (104 loc) · 3.67 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package liyf;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
/*Runnable runnable = new Runnable() {
private int ticket = 5;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (this) {
if (ticket > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ticket = " + ticket--);
}
}
}
}
};
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();*/
ExecutorService pool = Executors.newFixedThreadPool(2);
CountDownLatch doneSignal = new CountDownLatch(2);
VolatileFeatureTest vft = new VolatileFeatureTest(5);
pool.execute(new Worker(doneSignal, "worker1", vft));
pool.execute(new Worker(doneSignal, "worker2", vft));
doneSignal.await();
System.out.println("all of the threads are finished");
pool.shutdown();
}
// @Test
public void printThreadInfo() {
// get the manager(MXBean) of Java.
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
// don't need get the information of the monitor and synchronizer
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
for (ThreadInfo threadInfo : threadInfos) {
System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName());
}
}
public static final class Worker implements Runnable {
private final CountDownLatch doneSignal;
private VolatileFeatureTest vft;
private String workerName;
Worker(CountDownLatch doneSignal, String workerName, VolatileFeatureTest vft) {
this.doneSignal = doneSignal;
this.workerName = workerName;
this.vft = vft;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (vft.get() > 0) {
System.out
.println("Worker: " + workerName + " was running. " + "ticket = " + vft.getAndReduce());
} else {
break;
}
}
}
doneSignal.countDown();
}
}
/**
*
* Used volatile to ensure tickets synchronization in multi-thread
* @author vfaxian
*
*/
public static class VolatileFeatureTest {
private volatile int ticket;
public VolatileFeatureTest(int ticket) {
set(ticket);
}
public int get() {
return ticket;
}
public void set(int ticket) {
this.ticket = ticket;
}
public int getAndReduce() {
if (ticket < 1) {
return 0;
}
return ticket--;
}
}
}