GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
RunnableTask.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.io.PrintStream;
32
33import com.jogamp.common.JogampRuntimeException;
34
35/**
36 * Helper class to provide a Runnable queue implementation with a Runnable wrapper
37 * which notifies after execution for the <code>invokeAndWait()</code> semantics.
38 */
39public class RunnableTask extends TaskBase {
40 protected final Runnable runnable;
41
42 /**
43 * Invokes <code>runnable</code> on the current {@link Thread}.
44 * @param runnable the {@link Runnable} to execute on the current thread.
45 * The runnable <b>must exit</b>, i.e. not loop forever.
46 * @return the newly created and invoked {@link RunnableTask}
47 * @since 2.4.0
48 */
49 public static RunnableTask invokeOnCurrentThread(final Runnable runnable) {
50 final RunnableTask rt = new RunnableTask( runnable, null, false, null );
51 rt.run();
52 return rt;
53 }
54
55 /**
56 * Invokes <code>runnable</code> on a new {@link InterruptSource.Thread},
57 * see {@link InterruptSource.Thread#Thread(ThreadGroup, Runnable, String)} for details.
58 * @param tg the {@link ThreadGroup} for the new thread, maybe <code>null</code>
59 * @param threadName the name for the new thread, maybe <code>null</code>
60 * @param waitUntilDone if <code>true</code>, waits until <code>runnable</code> execution is completed, otherwise returns immediately.
61 * @param runnable the {@link Runnable} to execute on the new thread. If <code>waitUntilDone</code> is <code>true</code>,
62 * the runnable <b>must exit</b>, i.e. not loop forever.
63 * @return the newly created and invoked {@link RunnableTask}
64 * @since 2.3.2
65 */
66 public static RunnableTask invokeOnNewThread(final ThreadGroup tg, final String threadName,
67 final boolean waitUntilDone, final Runnable runnable) {
68 final RunnableTask rt;
69 if( !waitUntilDone ) {
70 rt = new RunnableTask( runnable, null, true, System.err );
71 final InterruptSource.Thread t = InterruptSource.Thread.create(tg, rt, threadName);
72 t.start();
73 } else {
74 final Object sync = new Object();
75 rt = new RunnableTask( runnable, sync, true, null );
76 final InterruptSource.Thread t = InterruptSource.Thread.create(tg, rt, threadName);
77 synchronized(sync) {
78 t.start();
79 while( rt.isInQueue() ) {
80 try {
81 sync.wait();
82 } catch (final InterruptedException ie) {
83 throw new InterruptedRuntimeException(ie);
84 }
85 final Throwable throwable = rt.getThrowable();
86 if(null!=throwable) {
87 throw new JogampRuntimeException(throwable);
88 }
89 }
90 }
91 }
92 return rt;
93 }
94
95 /**
96 * Create a RunnableTask object w/ synchronization,
97 * ie. suitable for <code>invokeAndWait()</code>, i.e. {@link #invoke(boolean, Runnable) invoke(true, runnable)}.
98 *
99 * @param runnable The user action
100 * @param syncObject The synchronization object if caller wait until <code>runnable</code> execution is completed,
101 * or <code>null</code> if waiting is not desired.
102 * @param catchExceptions Influence an occurring exception during <code>runnable</code> execution.
103 * If <code>true</code>, the exception is silenced and can be retrieved via {@link #getThrowable()},
104 * otherwise the exception is thrown.
105 * @param exceptionOut If not <code>null</code>, exceptions are written to this {@link PrintStream}.
106 */
107 public RunnableTask(final Runnable runnable, final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut) {
109 this.runnable = runnable ;
110 }
111
112 /** Return the user action */
113 public final Runnable getRunnable() {
114 return runnable;
115 }
116
117 @Override
118 public final void run() {
119 execThread = Thread.currentThread();
120
121 runnableException = null;
122 tStarted = System.currentTimeMillis();
123 if(null == syncObject) {
124 try {
125 runnable.run();
126 } catch (final Throwable t) {
128 if(null != exceptionOut) {
129 exceptionOut.println("RunnableTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString());
131 runnableException.printStackTrace(exceptionOut);
132 }
133 if(!catchExceptions) {
134 throw new RuntimeException(runnableException);
135 }
136 } finally {
137 tExecuted = System.currentTimeMillis();
138 isExecuted = true;
139 }
140 } else {
141 synchronized (syncObject) {
142 try {
143 runnable.run();
144 } catch (final Throwable t) {
146 if(null != exceptionOut) {
147 exceptionOut.println("RunnableTask.run(): "+getExceptionOutIntro()+" exception occured on thread "+Thread.currentThread().getName()+": "+toString());
149 t.printStackTrace(exceptionOut);
150 }
151 if(!catchExceptions) {
152 throw new RuntimeException(runnableException);
153 }
154 } finally {
155 tExecuted = System.currentTimeMillis();
156 isExecuted = true;
157 syncObject.notifyAll();
158 }
159 }
160 }
161 }
162}
163
A generic unchecked exception for Jogamp errors used throughout the binding as a substitute for Runti...
java.lang.Thread specialization implementing InterruptSource to track java.lang.Thread#interrupt() ca...
static Thread create(final ThreadGroup tg, final Runnable target, final String name)
Depending on whether name is null, either Thread(ThreadGroup, Runnable, String) or Thread(ThreadGroup...
Unchecked exception propagating an InterruptedException where handling of the latter is not desired.
Helper class to provide a Runnable queue implementation with a Runnable wrapper which notifies after ...
static RunnableTask invokeOnCurrentThread(final Runnable runnable)
Invokes runnable on the current Thread.
final Runnable getRunnable()
Return the user action.
RunnableTask(final Runnable runnable, final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut)
Create a RunnableTask object w/ synchronization, ie.
static RunnableTask invokeOnNewThread(final ThreadGroup tg, final String threadName, final boolean waitUntilDone, final Runnable runnable)
Invokes runnable on a new InterruptSource.Thread, see InterruptSource.Thread#Thread(ThreadGroup,...
Helper class to provide a Runnable queue implementation with a Runnable wrapper which notifies after ...
Definition: TaskBase.java:39
final String getExceptionOutIntro()
Definition: TaskBase.java:82
final Throwable getThrowable()
Definition: TaskBase.java:171
volatile boolean isExecuted
Definition: TaskBase.java:57
final PrintStream exceptionOut
Definition: TaskBase.java:50
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...