-
Notifications
You must be signed in to change notification settings - Fork 92
/
ManualResetEvent.java
48 lines (38 loc) · 1012 Bytes
/
ManualResetEvent.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
package com.github.segmentio.utils;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
// Thanks to Pelops for implmentation
// MIT License
// from: https://code.google.com/p/pelops/source/browse/trunk/src/org/wyki/concurrency/ManualResetEvent.java?r=2
public class ManualResetEvent {
private volatile CountDownLatch event;
private final Integer mutex;
public ManualResetEvent(boolean signalled) {
mutex = new Integer(-1);
if (signalled) {
event = new CountDownLatch(0);
} else {
event = new CountDownLatch(1);
}
}
public void set() {
event.countDown();
}
public void reset() {
synchronized (mutex) {
if (event.getCount() == 0) {
event = new CountDownLatch(1);
}
}
}
public void waitOne() throws InterruptedException {
event.await();
}
public boolean waitOne(int timeout, TimeUnit unit)
throws InterruptedException {
return event.await(timeout, unit);
}
public boolean isSignalled() {
return event.getCount() == 0;
}
}