From a764e59e1d3a8e330f0d484fdb26b35ca3f0b2e4 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sat, 22 Mar 2008 02:15:12 +0000 Subject: bringing pdj-0.8.3 into the main branch svn path=/trunk/externals/loaders/pdj/; revision=9621 --- src/java/com/cycling74/max/MaxQelem.java | 114 +++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/java/com/cycling74/max/MaxQelem.java (limited to 'src/java/com/cycling74/max/MaxQelem.java') diff --git a/src/java/com/cycling74/max/MaxQelem.java b/src/java/com/cycling74/max/MaxQelem.java new file mode 100644 index 0000000..100f671 --- /dev/null +++ b/src/java/com/cycling74/max/MaxQelem.java @@ -0,0 +1,114 @@ +package com.cycling74.max; + +/** + * Background job utility class. This is used to execute code in the + * background that might take time to execute. Calling set + * will trigger/execute the "job". If set is called while + * the "job" is running, the "job" won't be called again. + *

On PDJ, a Qelem is simply a Java thread that wait to be triggered. + *

+ */ +public class MaxQelem { + Thread job; + Executable exec; + boolean incall; + static int nbQelem = 0; + boolean stopThread = true; + + /** + * Constructs a Qelem that is bound the overriden class with method + * name qfn. + */ + public MaxQelem() { + exec = new Callback(this, "qfn"); + do_init(); + } + + /** + * Constructs a Qelem that is bound to an executable. + * @param exec the executable to run + */ + public MaxQelem(Executable exec) { + this.exec = exec; + do_init(); + } + + /** + * Constructs a Qelem that is bound to a class with method name. + * @param src the object that contains the method + * @param method the method to execute + */ + public MaxQelem(Object src, String method) { + exec = new Callback(src, method); + do_init(); + } + + private void do_init() { + job = new Thread(new Dispatcher(), "MaxQelem#" + (++nbQelem)); + job.setPriority(Thread.MIN_PRIORITY); + } + + /** + * Puts thread in front execution. Does nothing on PDJ + */ + public void front() { + } + + /** + * The callback method, otherwise it calls the 'executable' method + * provided in constructor. + */ + public void qfn() { + } + + /** + * Ask qfn to execute. If it is already set, the qfn won't be called + * twice. + */ + public synchronized void set() { + if ( !incall ) { + job.notify(); + } + } + + /** + * Cancels execution of qelem. Use with caution since it will throw + * an InterruptedException on PDJ. + */ + public synchronized void unset() { + job.interrupt(); + } + + + /** + * Releases current Qelem and cancel the running thread. + */ + public void release() { + unset(); + stopThread = true; + } + + /** + * Returns the executable object. + * @return the executble object that is bound to this Qelem + */ + public Executable getExecutable() { + return exec; + } + + class Dispatcher implements Runnable { + public void run() { + while( !stopThread ) { + try { + incall = false; + job.wait(); + incall = true; + exec.execute(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + } +} -- cgit v1.2.1