forked from seaswalker/jdk-sourcecode-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
109 lines (93 loc) · 3.15 KB
/
Test.java
File metadata and controls
109 lines (93 loc) · 3.15 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
package test;
import java.text.ParseException;
import java.util.concurrent.*;
/**
* Test something.
*
* @author skywalker
*/
public class Test {
/**
* Test {@link Integer#numberOfLeadingZeros(int)}.
*/
@org.junit.Test
public void leadingZeroes() throws ParseException {
System.out.println(Integer.numberOfLeadingZeros(16));
System.out.println(146 / 95.6666666667 >= 1.5);
}
@org.junit.Test
public void linkedQueue() {
SomeQueue<String> queue = new SomeQueue<>();
queue.offer("a");
System.out.println(queue.poll());
}
@org.junit.Test
public void threadPool() throws InterruptedException {
ThreadPoolExecutor service = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
service.execute(() -> {
throw new RuntimeException();
});
Thread.sleep(2000);
System.out.println(service.getPoolSize());
}
@org.junit.Test
public void maxPoolSize() throws InterruptedException {
ThreadPoolExecutor service = (ThreadPoolExecutor) new ThreadPoolExecutor(1, 3, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
service.execute(new StupidTask(1));
service.execute(new StupidTask(2));
service.execute(new StupidTask(3));
Thread.sleep(7000);
System.out.println(service.getPoolSize());
System.out.println(service.getLargestPoolSize());
}
@org.junit.Test
public void threadLocal() {
ThreadLocal<String> local = ThreadLocal.withInitial(() -> "hello");
}
private class StupidTask implements Runnable {
private final int id;
private StupidTask(int id) {
this.id = id;
}
@Override
public void run() {
System.out.println("hello" + id + ": " + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 如果我们向线程池submit的任务尚未被执行,同时有线程阻塞在{@link FutureTask#get()}方法上,那么当
* {@link ThreadPoolExecutor#shutdownNow()}方法调用时,阻塞的线程会被唤醒吗?
* <p>答案是不能.</p>
*/
@org.junit.Test
public void canWakeUp() throws InterruptedException, ExecutionException {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
executor.execute(new StupidTask(1));
Future<String> future = executor.submit(() -> "hello");
Thread.sleep(3000);
executor.shutdownNow();
future.get();
System.out.println("被唤醒");
}
private boolean flag = true;
@org.junit.Test
public void testVolatile() {
new Thread(() -> {
try {
System.out.println("子线程启动");
TimeUnit.SECONDS.sleep(3);
flag = false;
System.out.println("flag false");
} catch (InterruptedException ignore) {
}
}).start();
while (flag) {
System.out.print(1);
}
}
}