From 286f187df47f66ced7574976b908e3d4f4782523 Mon Sep 17 00:00:00 2001 From: "mahendran.mookkiah" Date: Fri, 15 Sep 2017 18:26:59 -0400 Subject: [PATCH] #587SonarQube reports bugs in promise module --- .../main/java/com/iluwatar/promise/App.java | 2 +- .../com/iluwatar/promise/PromiseSupport.java | 49 +++++++++---------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/promise/src/main/java/com/iluwatar/promise/App.java b/promise/src/main/java/com/iluwatar/promise/App.java index df4d0cc79..dd40ec15b 100644 --- a/promise/src/main/java/com/iluwatar/promise/App.java +++ b/promise/src/main/java/com/iluwatar/promise/App.java @@ -65,7 +65,7 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); - private static final String DEFAULT_URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md"; + private static final String DEFAULT_URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/promise/README.md"; private final ExecutorService executor; private final CountDownLatch stopLatch; diff --git a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java index 3629b4b5d..ae90a927e 100644 --- a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java +++ b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java @@ -27,11 +27,16 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * A really simplified implementation of future that allows completing it successfully with a value * or exceptionally with an exception. */ class PromiseSupport implements Future { + + private static final Logger LOGGER = LoggerFactory.getLogger(PromiseSupport.class); private static final int RUNNING = 1; private static final int FAILED = 2; @@ -80,40 +85,34 @@ class PromiseSupport implements Future { @Override public T get() throws InterruptedException, ExecutionException { - if (state == COMPLETED) { - return value; - } else if (state == FAILED) { - throw new ExecutionException(exception); - } else { - synchronized (lock) { + synchronized (lock) { + while (state == RUNNING) { lock.wait(); - if (state == COMPLETED) { - return value; - } else { - throw new ExecutionException(exception); - } } } + if (state == COMPLETED) { + return value; + } + throw new ExecutionException(exception); } @Override public T get(long timeout, TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - if (state == COMPLETED) { - return value; - } else if (state == FAILED) { - throw new ExecutionException(exception); - } else { - synchronized (lock) { - lock.wait(unit.toMillis(timeout)); - if (state == COMPLETED) { - return value; - } else if (state == FAILED) { - throw new ExecutionException(exception); - } else { - throw new TimeoutException(); + throws ExecutionException, TimeoutException { + synchronized (lock) { + while (state == RUNNING) { + try { + lock.wait(unit.toMillis(timeout)); + } catch (InterruptedException e) { + LOGGER.warn("Interrupted!", e); + Thread.currentThread().interrupt(); } } } + + if (state == COMPLETED) { + return value; + } + throw new ExecutionException(exception); } } \ No newline at end of file