diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-08-23 16:22:56 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-08-23 16:22:56 +0000 |
commit | 395e6be364eac3d7b7ce9449ac5ba5d4b0035760 (patch) | |
tree | 241a2f109b15e8911e3987a184529a74d7c7dfef /utils/src/main | |
parent | c60efde8a54d11fa17cfc83c185f3ca3f4c64709 (diff) | |
parent | 4564d7b3e8bad18432c817eb780400b220451f1a (diff) |
Merge "Prevent deadlock in ServiceManager"
Diffstat (limited to 'utils/src/main')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java index 1387462f..67a588fe 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/ServiceManager.java @@ -23,6 +23,7 @@ package org.onap.policy.common.utils.services; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; +import java.util.concurrent.atomic.AtomicBoolean; import lombok.AllArgsConstructor; import lombok.Getter; import org.onap.policy.common.capabilities.Startable; @@ -50,7 +51,7 @@ public class ServiceManager implements Startable { /** * {@code True} if the services are currently running, {@code false} otherwise. */ - private boolean running; + private final AtomicBoolean running = new AtomicBoolean(false); /** * Constructs the object, with a default name. @@ -77,7 +78,7 @@ public class ServiceManager implements Startable { * @return this manager */ public synchronized ServiceManager addAction(String stepName, RunnableWithEx starter, RunnableWithEx stopper) { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running; cannot add " + stepName); } @@ -94,7 +95,7 @@ public class ServiceManager implements Startable { * @return this manager */ public synchronized ServiceManager addService(String stepName, Startable service) { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running; cannot add " + stepName); } @@ -103,13 +104,13 @@ public class ServiceManager implements Startable { } @Override - public synchronized boolean isAlive() { - return running; + public boolean isAlive() { + return running.get(); } @Override public synchronized boolean start() { - if (running) { + if (isAlive()) { throw new IllegalStateException(name + " is already running"); } @@ -134,7 +135,7 @@ public class ServiceManager implements Startable { if (ex == null) { logger.info("{} started", name); - running = true; + running.set(true); return true; } @@ -151,11 +152,11 @@ public class ServiceManager implements Startable { @Override public synchronized boolean stop() { - if (!running) { + if (!isAlive()) { throw new IllegalStateException(name + " is not running"); } - running = false; + running.set(false); rewind(items); return true; |