From: Tomas Wenström Date: Mon, 2 Dec 2019 22:08:24 +0000 (+0100) Subject: Synchronize on threads on wait/notify X-Git-Url: http://dolda2000.com/gitweb/?p=kaka%2Fcakelight.git;a=commitdiff_plain;h=4c688086a9e836fa9c93e66eebf439db5406f3aa Synchronize on threads on wait/notify --- diff --git a/src/kaka/cakelight/mode/AmbientMode.java b/src/kaka/cakelight/mode/AmbientMode.java index ba816d5..9843084 100644 --- a/src/kaka/cakelight/mode/AmbientMode.java +++ b/src/kaka/cakelight/mode/AmbientMode.java @@ -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(); } } }; diff --git a/src/kaka/cakelight/mode/VideoMode.java b/src/kaka/cakelight/mode/VideoMode.java index c7985e3..41b599a 100644 --- a/src/kaka/cakelight/mode/VideoMode.java +++ b/src/kaka/cakelight/mode/VideoMode.java @@ -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 frame = grabber.grabFrame(); if (isPaused) { - wait(); + synchronized (grabberThread) { + wait(); + } } if (frameConsumer != null) frame.ifPresent(frameConsumer); frame.ifPresent(VideoMode.this::onVideoFrame);