invokeAndWait()
semantics.
*/
public class FunctionTaskfunc
.
* @param waitUntilDone if true
, waits until func
execution is completed, otherwise returns immediately.
* @param func the {@link Function} to execute.
* @param args the {@link Function} arguments
* @return the {@link Function} return value
*/
public static U invoke(boolean waitUntilDone, Function func, V... args) {
Throwable throwable = null;
final Object sync = new Object();
final FunctionTask rt = new FunctionTask( func, waitUntilDone ? sync : null, true );
final U res;
synchronized(sync) {
res = rt.eval(args);
if( waitUntilDone ) {
try {
sync.wait();
} catch (InterruptedException ie) {
throwable = ie;
}
if(null==throwable) {
throwable = rt.getThrowable();
}
if(null!=throwable) {
throw new RuntimeException(throwable);
}
}
}
return res;
}
/**
* Create a RunnableTask object w/ synchronization,
* ie. suitable for invokeAndWait()
.
*
* @param runnable the user action
* @param syncObject the synchronization object the caller shall wait until runnable
execution is completed,
* or null
if waiting is not desired.
* @param catchExceptions if true, exception during runnable
execution are catched, otherwise not.
* Use {@link #getThrowable()} to determine whether an exception has been catched.
*/
public FunctionTask(Function* Calls {@link #eval(Object...)}. *
** You may set the {@link #eval(Object...)} arguments via {@link #setArgs(Object...)} * and retrieve the result via {@link #getResult()}. *
*/ @Override public void run() { result = eval(args); args = null; } @Override public R eval(A... args) { R res = null; tStarted = System.currentTimeMillis(); if(null == syncObject) { try { res = runnable.eval(args); } catch (Throwable t) { runnableException = t; if(!catchExceptions) { throw new RuntimeException(runnableException); } } finally { tExecuted = System.currentTimeMillis(); } } else { synchronized (syncObject) { try { res = runnable.eval(args); } catch (Throwable t) { runnableException = t; if(!catchExceptions) { throw new RuntimeException(runnableException); } } finally { tExecuted = System.currentTimeMillis(); syncObject.notifyAll(); } } } return res; } }