From f39100b35d0833764f2220e487ea7ea05ed87352 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 11 May 2014 23:36:48 +0200 Subject: RunnableTask: Add static method 'invokeOnNewThread(..)' for convenience (Used in JOGL to mitigate Bug 1004) --- src/java/com/jogamp/common/util/RunnableTask.java | 40 ++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/java') 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 runnable. + * Invokes runnable on the current thread. * @param waitUntilDone if true, waits until runnable execution is completed, otherwise returns immediately. * @param runnable the {@link Runnable} to execute. */ @@ -64,6 +64,44 @@ public class RunnableTask extends TaskBase { } } + /** + * Invokes runnable on a new thread belonging to the given {@link ThreadGroup}. + * @param tg the {@link ThreadGroup} for the new thread, maybe null + * @param waitUntilDone if true, waits until runnable execution is completed, otherwise returns immediately. + * @param runnable the {@link Runnable} to execute on the new thread. If waitUntilDone is true, + * the runnable must exist, 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 invokeAndWait(), i.e. {@link #invoke(boolean, Runnable) invoke(true, runnable)}. -- cgit v1.2.3