From 7be826174d1837629c0ad33c7b6d4dfb3bc63a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 28 Feb 2012 14:17:17 +0100 Subject: Add VariableUpdaterTask. VariableUpdaterTask updates a variable countinously. --- .../java/com/orbekk/same/VariableUpdaterTask.java | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 same/src/main/java/com/orbekk/same/VariableUpdaterTask.java (limited to 'same/src/main/java') diff --git a/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java b/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java new file mode 100644 index 0000000..44f63fe --- /dev/null +++ b/same/src/main/java/com/orbekk/same/VariableUpdaterTask.java @@ -0,0 +1,63 @@ +package com.orbekk.same; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** Updates a variable on-demand. + */ +public class VariableUpdaterTask extends Thread + implements Variable.OnChangeListener { + private Variable variable; + private T newValue; + private AtomicBoolean hasNewValue = new AtomicBoolean(false); + private AtomicBoolean isReady = new AtomicBoolean(true); + + public VariableUpdaterTask(Variable variable) { + this.variable = variable; + } + + public synchronized void set(T newValue) { + this.newValue = newValue; + hasNewValue.set(true); + notifyAll(); + } + + /** Update the variable once. */ + public void performWork() { + boolean shouldDoWork = false; + synchronized(this) { + shouldDoWork = hasNewValue.get() && isReady.get(); + hasNewValue.set(false); + isReady.set(false); + } + if (shouldDoWork) { + variable.set(newValue); + } + } + + private synchronized void waitFor(AtomicBoolean v) { + while(!v.get()) { + try { + wait(); + } catch (InterruptedException e) { + return; + } + } + } + + public void run() { + while (true) { + waitFor(isReady); + waitFor(hasNewValue); + if (Thread.interrupted()) { + break; + } + performWork(); + } + } + + @Override + public synchronized void valueChanged(Variable unused) { + isReady.set(true); + notifyAll(); + } +} \ No newline at end of file -- cgit v1.2.3