-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeoutPropertyExample.java
102 lines (84 loc) · 2.56 KB
/
TimeoutPropertyExample.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
package example;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.shimizukenta.property.BooleanProperty;
import com.shimizukenta.property.TimeoutProperty;
public class TimeoutPropertyExample implements Runnable {
public TimeoutPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build instance: 1L, TimeUnit.MINUTES");
TimeoutProperty p = TimeoutProperty.newInstance(1L, TimeUnit.MINUTES);
System.out.println("get: " + p.get());
System.out.println();
System.out.println("set: 10(int)");
p.set(10);
System.out.println("get: " + p);
System.out.println();
System.out.println("set: 5.0F(float)");
p.set(5.0F);
System.out.println("get: " + p);
System.out.println();
System.out.println("set: 2.0D(double)");
p.set(2.0D);
System.out.println("get: " + p);
System.out.println();
/* TimeUnit#sleep */
System.out.println("TimeUnit.sleep " + p + "...");
p.sleep();
System.out.println("wakeup");
System.out.println();
/* TimeUnit#wait */
System.out.println("TimeUnit.wait(this) " + p + "...");
synchronized ( this ) {
p.wait(this);
System.out.println("timeout");
}
System.out.println();
/* BlockingQueue#poll */
System.out.println("BlockingQueue.poll(timoeut, TimeUnit) " + p + "...");
{
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
String r = p.blockingQueuePoll(queue);
if ( r == null ) {
System.out.println("timeout, BlockingQueue#poll returns " + r);
} else {
System.out.println("poll is " + r);
}
}
System.out.println();
/* BooleanProperty#waitUntil */
try {
System.out.println("build false BooleanProperty");
BooleanProperty bool = BooleanProperty.newInstance(false);
System.out.println("get bool: " + bool.booleanValue());
System.out.println("BooleanProperty#waitUntilTrue with TimeoutProperty...");
bool.waitUntilTrue(p);
}
catch ( TimeoutException e ) {
System.out.println("timeout, Boolean#waitUntilTrue " + p);
System.out.println(e);
}
System.out.println();
System.out.println("reach end");
}
catch ( InterruptedException ignore ) {
}
}
public static void main(String[] args) {
try {
new TimeoutPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}