Synchronize on threads on wait/notify
authorTomas Wenström <tomas@cakepi>
Mon, 2 Dec 2019 22:08:24 +0000 (23:08 +0100)
committerTomas Wenström <tomas@cakepi>
Mon, 2 Dec 2019 22:08:24 +0000 (23:08 +0100)
src/kaka/cakelight/mode/AmbientMode.java
src/kaka/cakelight/mode/VideoMode.java

index ba816d5..9843084 100644 (file)
@@ -33,7 +33,9 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
     @Override
     public void resume() {
         isPaused = false;
-        thread.notify();
+       synchronized (thread) {
+           thread.notify();
+       }
     }
 
     @Override
@@ -49,7 +51,9 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
                     int index = 0;
                     while (!isInterrupted()) {
                         if (isPaused) {
-                            wait();
+                           synchronized (thread) {
+                               wait();
+                           }
                         }
                         LedFrame frame = LedFrame.from(config);
                         updateFrame(frame, System.currentTimeMillis() - start, index);
@@ -58,7 +62,7 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
                         Thread.sleep(20);
                     }
                 } catch (InterruptedException e) {
-                    e.printStackTrace();
+                    // e.printStackTrace();
                 }
             }
         };
index c7985e3..41b599a 100644 (file)
@@ -36,7 +36,9 @@ public class VideoMode extends Mode {
     @Override
     public void resume() {
         isPaused = false;
-        grabberThread.notify();
+       synchronized (grabberThread) {
+           grabberThread.notify();
+       }
     }
 
     @Override
@@ -53,7 +55,9 @@ public class VideoMode extends Mode {
                     while (!isInterrupted()) {
                         Optional<VideoFrame> frame = grabber.grabFrame();
                         if (isPaused) {
-                            wait();
+                           synchronized (grabberThread) {
+                               wait();
+                           }
                         }
                         if (frameConsumer != null) frame.ifPresent(frameConsumer);
                         frame.ifPresent(VideoMode.this::onVideoFrame);