Removed throwing of runtime exceptions
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / repository / Lock.java
index ff3f331..def2b30 100644 (file)
@@ -59,11 +59,16 @@ public class Lock {
 
         @Override
         public void run() {
-            while (true) {
-                for (LockRequest request : consume()) {
-                    request.callback.success(request.lock);
+            try {
+                while (true) {
+                    for (LockRequest request : consume()) {
+                        request.callback.success(request.lock);
+                    }
+                    waitForNewEntries();
                 }
-                waitForNewEntries();
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                logger.error("Interrupted {}", e.getMessage());
             }
         }
 
@@ -74,14 +79,9 @@ public class Lock {
         }
 
         @SuppressWarnings("java:S2274")
-        private synchronized void waitForNewEntries() {
-            try {
-                if (this.lockRequestQueue.isEmpty()) {
-                    this.wait();
-                }
-            } catch (InterruptedException e) {
-                logger.warn("waitForUnlock interrupted", e);
-                Thread.currentThread().interrupt();
+        private synchronized void waitForNewEntries() throws InterruptedException {
+            if (this.lockRequestQueue.isEmpty()) {
+                this.wait();
             }
         }
     }
@@ -117,7 +117,7 @@ public class Lock {
         synchronized (this) {
             if (lockCounter <= 0) {
                 lockCounter = -1; // Might as well stop, to make it easier to find the problem
-                throw new NullPointerException("Number of unlocks must match the number of locks");
+                logger.error("Number of unlocks must match the number of locks");
             }
             this.lockCounter--;
             if (lockCounter == 0) {
@@ -168,7 +168,7 @@ public class Lock {
         lockRequestQueue.add(new LockRequest(callback, lockType, this));
     }
 
-    @SuppressWarnings("java:S2274")
+    @SuppressWarnings("java:S2274") // Always invoke wait() and await() methods inside a loop
     private synchronized void waitForUnlock() {
         try {
             this.wait();