diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-05-01 12:25:08 +0200 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-05-01 12:25:08 +0200 |
commit | 84ac5627e8bfd824f1885931a18972731b478009 (patch) | |
tree | 6d7d5dcfa66eb6ecee07989653d373ce7fead5ae /same/src/main/java/com/orbekk/util | |
parent | 0bc8c4ddc0ffe6febf8cf39c6144e0193957a96a (diff) |
Fix DelayedOperation.
Use a CountDownLatch instead of manual synchronization.
Diffstat (limited to 'same/src/main/java/com/orbekk/util')
-rw-r--r-- | same/src/main/java/com/orbekk/util/DelayedOperation.java | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/same/src/main/java/com/orbekk/util/DelayedOperation.java b/same/src/main/java/com/orbekk/util/DelayedOperation.java index c423a7d..7a7b808 100644 --- a/same/src/main/java/com/orbekk/util/DelayedOperation.java +++ b/same/src/main/java/com/orbekk/util/DelayedOperation.java @@ -1,5 +1,7 @@ package com.orbekk.util; +import java.util.concurrent.CountDownLatch; + public class DelayedOperation { public static class Status { public final static int OK = 1; @@ -65,10 +67,10 @@ public class DelayedOperation { } } - private Status status; - private boolean isDone; - private int identifier; - + private volatile Status status; + private volatile int identifier; + private final CountDownLatch done = new CountDownLatch(1); + public DelayedOperation() { } @@ -77,26 +79,22 @@ public class DelayedOperation { return status; } - public synchronized void waitFor() { - while (!isDone) { - try { - wait(); - } catch (InterruptedException e) { - complete(new Status(Status.ERROR, "Thread interrupted.")); - } + public void waitFor() { + try { + done.await(); + } catch (InterruptedException e) { + complete(new Status(Status.ERROR, "Thread interrupted.")); + Thread.currentThread().interrupt(); } } public synchronized boolean isDone() { - return isDone; + return done.getCount() <= 0; } public synchronized void complete(Status status) { - if (!isDone) { - isDone = true; - this.status = status; - notifyAll(); - } + this.status = status; + done.countDown(); } public synchronized int getIdentifier() { |