From 84ac5627e8bfd824f1885931a18972731b478009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 1 May 2012 12:25:08 +0200 Subject: Fix DelayedOperation. Use a CountDownLatch instead of manual synchronization. --- .../java/com/orbekk/util/DelayedOperation.java | 32 ++++++++++------------ 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'same') 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() { -- cgit v1.2.3