diff options
author | Sven Gothel <[email protected]> | 2014-05-11 23:36:48 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-05-11 23:36:48 +0200 |
commit | f39100b35d0833764f2220e487ea7ea05ed87352 (patch) | |
tree | 382d79752abbf9c781aea1454a94ad95bf3bc736 /src/java/com/jogamp/common | |
parent | d2b21db6b0a6c7cb7dc12dbaa32ff47c579273bc (diff) |
RunnableTask: Add static method 'invokeOnNewThread(..)' for convenience (Used in JOGL to mitigate Bug 1004)
Diffstat (limited to 'src/java/com/jogamp/common')
-rw-r--r-- | src/java/com/jogamp/common/util/RunnableTask.java | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/java/com/jogamp/common/util/RunnableTask.java b/src/java/com/jogamp/common/util/RunnableTask.java index 1db4810..97adf04 100644 --- a/src/java/com/jogamp/common/util/RunnableTask.java +++ b/src/java/com/jogamp/common/util/RunnableTask.java @@ -38,7 +38,7 @@ public class RunnableTask extends TaskBase { protected final Runnable runnable; /** - * Invoks <code>runnable</code>. + * Invokes <code>runnable</code> on the current thread. * @param waitUntilDone if <code>true</code>, waits until <code>runnable</code> execution is completed, otherwise returns immediately. * @param runnable the {@link Runnable} to execute. */ @@ -65,6 +65,44 @@ public class RunnableTask extends TaskBase { } /** + * Invokes <code>runnable</code> on a new thread belonging to the given {@link ThreadGroup}. + * @param tg the {@link ThreadGroup} for the new thread, maybe <code>null</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 on the new thread. If <code>waitUntilDone</code> is <code>true</code>, + * the runnable <b>must exist</b>, i.e. not loop forever. + * @param threadName the name for the new thread + * @return the newly created {@link Thread} + */ + public static Thread invokeOnNewThread(final ThreadGroup tg, final boolean waitUntilDone, final Runnable runnable, final String threadName) { + final Thread t = new Thread(tg, threadName) { + @Override + public void run() { + Throwable throwable = null; + final Object sync = new Object(); + final RunnableTask rt = new RunnableTask( runnable, waitUntilDone ? sync : null, true, waitUntilDone ? null : System.err ); + 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); + } + } + } + } }; + t.start(); + return t; + } + + + /** * Create a RunnableTask object w/ synchronization, * ie. suitable for <code>invokeAndWait()</code>, i.e. {@link #invoke(boolean, Runnable) invoke(true, runnable)}. * |