From 4c688086a9e836fa9c93e66eebf439db5406f3aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Mon, 2 Dec 2019 23:08:24 +0100 Subject: [PATCH] Synchronize on threads on wait/notify --- src/kaka/cakelight/mode/AmbientMode.java | 10 +++++++--- src/kaka/cakelight/mode/VideoMode.java | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) 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); -- 2.11.0