Repository navigation
Expand file tree
/
Copy pathLongTimeouts.java
More file actions
50 lines (47 loc) · 1.77 KB
/
Copy pathLongTimeouts.java
File metadata and controls
50 lines (47 loc) · 1.77 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
import java.util.concurrent.locks.LockSupport;
// Timeouts longer than a host timer can hold must not wake early.
public class LongTimeouts {
static volatile boolean interrupting;
static String how(boolean interrupted) {
return interrupting && interrupted ? "interrupt" : "early";
}
public static void main(String[] args) throws Exception {
String[] result = new String[4];
Object lock = new Object();
Thread[] threads = {
new Thread(() -> {
LockSupport.parkNanos(Long.MAX_VALUE);
result[0] = how(Thread.currentThread().isInterrupted());
}),
new Thread(() -> {
LockSupport.parkUntil(Long.MAX_VALUE);
result[1] = how(Thread.currentThread().isInterrupted());
}),
new Thread(() -> {
synchronized (lock) {
try {
lock.wait(Long.MAX_VALUE / 2);
result[2] = "early";
} catch (InterruptedException e) {
result[2] = how(true);
}
}
}),
new Thread(() -> {
try {
Thread.sleep(Long.MAX_VALUE);
result[3] = "early";
} catch (InterruptedException e) {
result[3] = how(true);
}
}),
};
for (Thread t : threads) t.start();
Thread.sleep(300);
interrupting = true;
for (Thread t : threads) t.interrupt();
for (Thread t : threads) t.join();
String[] names = { "parkNanos", "parkUntil", "wait", "sleep" };
for (int i = 0; i < 4; i++) System.out.println(names[i] + ": " + result[i]);
}
}