added an ability to use a separate object for a lock.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@31751 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2010-06-05 06:06:52 +00:00
1 parent 82252f7d56
commit b682a5b28d
1 file changed
+29 -12
@@ -39,14 +39,25 @@ package hudson.util;
*/
public final class OneShotEvent {
private boolean signaled;
private final Object lock;
public OneShotEvent() {
this.lock = this;
}
public OneShotEvent(Object lock) {
this.lock = lock;
}
/**
* Non-blocking method that signals this event.
*/
public synchronized void signal() {
if(signaled) return;
this.signaled = true;
notifyAll();
public void signal() {
synchronized (lock) {
if(signaled) return;
this.signaled = true;
lock.notifyAll();
}
}
/**
@@ -55,9 +66,11 @@ public final class OneShotEvent {
* <p>
* This method blocks infinitely until a value is offered.
*/
public synchronized void block() throws InterruptedException {
while(!signaled)
wait();
public void block() throws InterruptedException {
synchronized (lock) {
while(!signaled)
lock.wait();
}
}
/**
@@ -67,15 +80,19 @@ public final class OneShotEvent {
* If the specified amount of time elapses,
* this method returns null even if the value isn't offered.
*/
public synchronized void block(long timeout) throws InterruptedException {
if(!signaled)
wait(timeout);
public void block(long timeout) throws InterruptedException {
synchronized (lock) {
if(!signaled)
lock.wait(timeout);
}
}
/**
* Returns true if a value is offered.
*/
public synchronized boolean isSignaled() {
return signaled;
public boolean isSignaled() {
synchronized (lock) {
return signaled;
}
}
}