Synchronize on threads on wait/notify
[kaka/cakelight.git] / src / kaka / cakelight / mode / AmbientMode.java
index 77f1bc3..9843084 100644 (file)
@@ -9,6 +9,7 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
     private Thread thread; // TODO move to a dynamic sub class
     protected Configuration config;
     private int type = 0;
+    private boolean isPaused = false;
 
     AmbientMode() {}
 
@@ -26,16 +27,15 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
 
     @Override
     public void pause() {
-        try {
-            thread.wait();
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
+        isPaused = true;
     }
 
     @Override
     public void resume() {
-        thread.notify();
+        isPaused = false;
+       synchronized (thread) {
+           thread.notify();
+       }
     }
 
     @Override
@@ -43,13 +43,18 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
         stopThread();
     }
 
-    public void startThread() {
+    private void startThread() {
         thread = new Thread() {
             public void run() {
                 try {
                     long start = System.currentTimeMillis();
                     int index = 0;
                     while (!isInterrupted()) {
+                        if (isPaused) {
+                           synchronized (thread) {
+                               wait();
+                           }
+                        }
                         LedFrame frame = LedFrame.from(config);
                         updateFrame(frame, System.currentTimeMillis() - start, index);
                         updateWithFrame(frame);
@@ -57,13 +62,14 @@ public class AmbientMode extends Mode { // TODO split into DynamicAmbient and St
                         Thread.sleep(20);
                     }
                 } catch (InterruptedException e) {
+                    // e.printStackTrace();
                 }
             }
         };
         thread.start();
     }
 
-    public void stopThread() {
+    private void stopThread() {
         thread.interrupt();
     }