aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/util/RunnableTask.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2013-02-13 13:04:27 +0100
committerSven Gothel <[email protected]>2013-02-13 13:04:27 +0100
commitb387d012103a02eb7d5eb919306583295ef09a38 (patch)
tree2e01b804cc071e0350ec72059cbd470d23c596e0 /src/java/com/jogamp/common/util/RunnableTask.java
parent30841742e735e70b3946d16711089960084e894c (diff)
Adding Function and FunctionTask extending RunnableTask functionality
Function allows passing arguments and having a return value in contrast to Runnable, where FunctionTask allows a Function to be invoked and waited for.
Diffstat (limited to 'src/java/com/jogamp/common/util/RunnableTask.java')
-rw-r--r--src/java/com/jogamp/common/util/RunnableTask.java151
1 files changed, 33 insertions, 118 deletions
diff --git a/src/java/com/jogamp/common/util/RunnableTask.java b/src/java/com/jogamp/common/util/RunnableTask.java
index b0ce159..6a4ccb3 100644
--- a/src/java/com/jogamp/common/util/RunnableTask.java
+++ b/src/java/com/jogamp/common/util/RunnableTask.java
@@ -32,55 +32,49 @@ package com.jogamp.common.util;
* Helper class to provide a Runnable queue implementation with a Runnable wrapper
* which notifies after execution for the <code>invokeAndWait()</code> semantics.
*/
-public class RunnableTask implements Runnable {
- Runnable runnable;
- final Object syncObject;
- boolean catchExceptions;
- Object attachment;
-
- Throwable runnableException;
- long tCreated, tStarted;
- volatile long tExecuted;
- volatile boolean isFlushed;
-
- /**
- * Create a RunnableTask object w/o synchronization,
- * ie. not suitable for <code>invokeAndWait()</code>.
- *
- * @param runnable the user action
- */
- public RunnableTask(Runnable runnable) {
- this(runnable, null, false);
- }
+public class RunnableTask extends TaskBase {
+ protected final Runnable runnable;
/**
- * Create a RunnableTask object w/ synchronization,
- * ie. suitable for <code>invokeAndWait()</code>.
- *
- * @param runnable the user action
- * @param syncObject the synchronization object the caller shall wait for in case of <code>invokeAndWait()</code>
+ * Invoks <code>runnable</code>.
+ * @param waitUntilDone if <code>true</code>, waits until <code>runnable</code> execution is completed, otherwise returns immediately.
+ * @param runnable the {@link Runnable} to execute.
*/
- public RunnableTask(Runnable runnable, Object syncObject) {
- this(runnable, syncObject, false);
+ public static void invoke(boolean waitUntilDone, Runnable runnable) {
+ Throwable throwable = null;
+ final Object sync = new Object();
+ final RunnableTask rt = new RunnableTask( runnable, waitUntilDone ? sync : null, true );
+ synchronized(sync) {
+ rt.run();
+ if( waitUntilDone ) {
+ try {
+ sync.wait();
+ } catch (InterruptedException ie) {
+ throwable = ie;
+ }
+ if(null==throwable) {
+ throwable = rt.getThrowable();
+ }
+ if(null!=throwable) {
+ throw new RuntimeException(throwable);
+ }
+ }
+ }
}
-
+
/**
* Create a RunnableTask object w/ synchronization,
- * ie. suitable for <code>invokeAndWait()</code>.
+ * ie. suitable for <code>invokeAndWait()</code>, i.e. {@link #invoke(boolean, Runnable) invoke(true, runnable)}.
*
* @param runnable the user action
- * @param syncObject the synchronization object the caller shall wait for in case of <code>invokeAndWai()t</code>
+ * @param syncObject the synchronization object if caller wait until <code>runnable</code> execution is completed,
+ * or <code>null</code> if waiting is not desired.
* @param catchExceptions if true, exception during <code>runnable</code> execution are catched, otherwise not.
* Use {@link #getThrowable()} to determine whether an exception has been catched.
*/
public RunnableTask(Runnable runnable, Object syncObject, boolean catchExceptions) {
+ super(syncObject, catchExceptions);
this.runnable = runnable ;
- this.syncObject = syncObject ;
- this.catchExceptions = catchExceptions ;
- tCreated = System.currentTimeMillis();
- tStarted = 0;
- tExecuted = 0;
- isFlushed = false;
}
/** Return the user action */
@@ -88,30 +82,7 @@ public class RunnableTask implements Runnable {
return runnable;
}
- /**
- * Return the synchronization object if any.
- * @see #RunnableTask(Runnable, Object, boolean)
- */
- public Object getSyncObject() {
- return syncObject;
- }
-
- /**
- * Attach a custom object to this task.
- * Useful to piggybag further information, ie tag a task final.
- */
- public void setAttachment(Object o) {
- attachment = o;
- }
-
- /**
- * Return the attachment object if any.
- * @see #setAttachment(Object)
- */
- public Object getAttachment() {
- return attachment;
- }
-
+ @Override
public void run() {
tStarted = System.currentTimeMillis();
if(null == syncObject) {
@@ -139,63 +110,7 @@ public class RunnableTask implements Runnable {
syncObject.notifyAll();
}
}
- }
- }
-
- /**
- * Simply flush this task and notify a waiting executor.
- * The executor which might have been blocked until notified
- * will be unblocked and the task removed from the queue.
- *
- * @see #isFlushed()
- * @see #isInQueue()
- */
- public void flush() {
- if(!isExecuted() && hasWaiter()) {
- synchronized (syncObject) {
- isFlushed = true;
- syncObject.notifyAll();
- }
- }
- }
-
- /**
- * @return !{@link #isExecuted()} && !{@link #isFlushed()}
- */
- public boolean isInQueue() { return 0 != tExecuted && !isFlushed; }
-
- /**
- * @return True if executed, otherwise false;
- */
- public boolean isExecuted() { return 0 != tExecuted ; }
-
- /**
- * @return True if flushed, otherwise false;
- */
- public boolean isFlushed() { return isFlushed; }
-
- /**
- * @return True if invoking thread waits until done,
- * ie a <code>notifyObject</code> was passed, otherwise false;
- */
- public boolean hasWaiter() { return null != syncObject; }
-
- /**
- * @return A thrown exception while execution of the user action, if any and if catched
- * @see #RunnableTask(Runnable, Object, boolean)
- */
- public Throwable getThrowable() { return runnableException; }
-
- public long getTimestampCreate() { return tCreated; }
- public long getTimestampBeforeExec() { return tStarted; }
- public long getTimestampAfterExec() { return tExecuted; }
- public long getDurationInQueue() { return tStarted - tCreated; }
- public long getDurationInExec() { return tExecuted - tStarted; }
- public long getDurationTotal() { return tExecuted - tCreated; }
-
- @Override
- public String toString() {
- return "RunnableTask[executed "+isExecuted()+", t2-t0 "+getDurationTotal()+", t2-t1 "+getDurationInExec()+", t1-t0 "+getDurationInQueue()+", throwable "+getThrowable()+", Runnable "+runnable+", Attachment "+attachment+"]";
- }
+ }
+ }
}