/
ThreadTest.java
193 lines (164 loc) · 5.55 KB
/
ThreadTest.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package util;
import org.junit.Test;
import java.util.Random;
import java.util.concurrent.*;
public class ThreadTest {
@Test
public void test() {
Thread t = Thread.currentThread();
System.out.println("id: " + t.getId());
System.out.println("name: " + t.getName());
System.out.println("priority: " + t.getPriority());
System.out.println("state: " + t.getState());
System.out.println("isAlive: " + t.isAlive());
System.out.println("isDaemon: " + t.isDaemon());
System.out.println("isInterrupted: " + t.isInterrupted());
System.out.println("threadGroup: " + t.getThreadGroup());
System.out.println("contextClassLoader: " + t.getContextClassLoader());
System.out.println("stackTrace: " + t.getStackTrace());
System.out.println("uncaughtExceptionHandler: " + t.getUncaughtExceptionHandler());
}
@Test
public void testThread() {
class MyThread extends Thread {
private final String name;
private MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
System.out.printf("%s is running %d\n", name, i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
{
Thread t1 = new MyThread("t1");
Thread t2 = new MyThread("t2");
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void testRunnable() {
class MyRunnable implements Runnable {
private final String name;
private MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
System.out.printf("%s is running %d\n", name, i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
{
Thread r1 = new Thread(new MyRunnable("r1"));
Thread r2 = new Thread(new MyRunnable("r2"));
r1.start();
r2.start();
try {
r1.join();
r2.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Test
public void testCallable() {
class MyCallable implements Callable<Integer> {
private final Random random;
private MyCallable() {
random = new Random();
}
@Override
public Integer call() throws Exception {
Thread.sleep(100);
return random.nextInt();
}
}
{
FutureTask<Integer> future1 = new FutureTask<>(new MyCallable());
FutureTask<Integer> future2 = new FutureTask<>(new MyCallable());
new Thread(future1).start();
new Thread(future2).start();
try {
System.out.println(future1.get(50, TimeUnit.MILLISECONDS));
} catch (InterruptedException | TimeoutException | ExecutionException e) {
e.printStackTrace();
}
try {
System.out.println(future2.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
@Test
public void testThreadPool() {
class MyCallable implements Callable<Integer> {
private final Random random;
private MyCallable() {
random = new Random();
}
@Override
public Integer call() throws Exception {
Thread.sleep(100L);
return random.nextInt();
}
}
{
ExecutorService es = Executors.newFixedThreadPool(5);
Future<Integer> future1 = es.submit(new MyCallable());
Future<Integer> future2 = es.submit(new MyCallable());
try {
System.out.println(future1.get(50, TimeUnit.MILLISECONDS));
} catch (TimeoutException e) {
System.out.println("future1 timeout");
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println(future2.get());
} catch (Exception e) {
e.printStackTrace();
}
es.shutdown();
}
{
ExecutorService es = Executors.newCachedThreadPool();
CompletionService<Integer> cs = new ExecutorCompletionService<>(es);
cs.submit(new MyCallable());
cs.submit(new MyCallable());
cs.submit(new MyCallable());
cs.submit(new MyCallable());
try {
System.out.println(cs.take().get());
System.out.println(cs.take().get());
System.out.println(cs.take().get());
System.out.println(cs.take().get());
} catch (Exception e) {
e.printStackTrace();
}
es.shutdown();
}
}
}