GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TaskBase.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 jogamp.common.Debug;
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 abstract class TaskBase implements Runnable {
40 /** Enable via the property <code>jogamp.debug.TaskBase.TraceSource</code> */
41 private static final boolean TRACE_SOURCE;
42
43 static {
44 Debug.initSingleton();
45 TRACE_SOURCE = PropertyAccess.isPropertyDefined("jogamp.debug.TaskBase.TraceSource", true);
46 }
47
48 protected final Object syncObject;
49 protected final boolean catchExceptions;
50 protected final PrintStream exceptionOut;
51 protected final Throwable sourceStack;
52
53 protected Object attachment;
54 protected Throwable runnableException;
55 protected long tCreated, tStarted;
56 protected volatile long tExecuted;
57 protected volatile boolean isExecuted;
58 protected volatile boolean isFlushed;
59 protected volatile Thread execThread;
60
61 /**
62 * @param syncObject The synchronization object if caller wait until <code>runnable</code> execution is completed,
63 * or <code>null</code> if waiting is not desired.
64 * @param catchExceptions Influence an occurring exception during <code>runnable</code> execution.
65 * If <code>true</code>, the exception is silenced and can be retrieved via {@link #getThrowable()},
66 * otherwise the exception is thrown.
67 * @param exceptionOut If not <code>null</code>, exceptions are written to this {@link PrintStream}.
68 */
69 protected TaskBase(final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut) {
70 this.syncObject = syncObject;
71 this.catchExceptions = catchExceptions;
72 this.exceptionOut = exceptionOut;
73 this.sourceStack = TRACE_SOURCE ? new Throwable("Creation @") : null;
74 this.tCreated = System.currentTimeMillis();
75 this.tStarted = 0;
76 this.tExecuted = 0;
77 this.isExecuted = false;
78 this.isFlushed = false;
79 this.execThread = null;
80 }
81
82 protected final String getExceptionOutIntro() {
83 return catchExceptions ? "A caught" : "An uncaught";
84 }
85 protected final void printSourceTrace() {
86 if( null != sourceStack && null != exceptionOut ) {
87 sourceStack.printStackTrace(exceptionOut);
88 }
89 }
90
91 /**
92 * Returns the execution thread or {@code null} if not yet {@link #run()}.
93 * @since 2.3.2
94 */
95 public final Thread getExecutionThread() {
96 return execThread;
97 }
98
99 /**
100 * Return the synchronization object if any.
101 * @see #RunnableTask(Runnable, Object, boolean)
102 */
103 public final Object getSyncObject() {
104 return syncObject;
105 }
106
107 /**
108 * Attach a custom object to this task.
109 * Useful to piggybag further information, ie tag a task final.
110 */
111 public final void setAttachment(final Object o) {
112 attachment = o;
113 }
114
115 /**
116 * Return the attachment object if any.
117 * @see #setAttachment(Object)
118 */
119 public final Object getAttachment() {
120 return attachment;
121 }
122
123 @Override
124 public abstract void run();
125
126 /**
127 * Simply flush this task and notify a waiting executor.
128 * The executor which might have been blocked until notified
129 * will be unblocked and the task removed from the queue.
130 *
131 * @param t optional Throwable to be assigned for later {@link #getThrowable()} query in case of an error.
132 *
133 * @see #isFlushed()
134 * @see #isInQueue()
135 */
136 public final void flush(final Throwable t) {
137 if(!isExecuted() && hasWaiter()) {
139 synchronized (syncObject) {
140 isFlushed = true;
141 syncObject.notifyAll();
142 }
143 }
144 }
145
146 /**
147 * @return !{@link #isExecuted()} && !{@link #isFlushed()}
148 */
149 public final boolean isInQueue() { return !isExecuted && !isFlushed; }
150
151 /**
152 * @return True if executed, otherwise false;
153 */
154 public final boolean isExecuted() { return isExecuted; }
155
156 /**
157 * @return True if flushed, otherwise false;
158 */
159 public final boolean isFlushed() { return isFlushed; }
160
161 /**
162 * @return True if invoking thread waits until done,
163 * ie a <code>notifyObject</code> was passed, otherwise false;
164 */
165 public final boolean hasWaiter() { return null != syncObject; }
166
167 /**
168 * @return A thrown exception while execution of the user action, if any and if caught
169 * @see #RunnableTask(Runnable, Object, boolean)
170 */
171 public final Throwable getThrowable() { return runnableException; }
172
173 public final long getTimestampCreate() { return tCreated; }
174 public final long getTimestampBeforeExec() { return tStarted; }
175 public final long getTimestampAfterExec() { return tExecuted; }
176 public final long getDurationInQueue() { return tStarted - tCreated; }
177 public final long getDurationInExec() { return 0 < tExecuted ? tExecuted - tStarted : 0; }
178 public final long getDurationTotal() { return 0 < tExecuted ? tExecuted - tCreated : tStarted - tCreated; }
179
180 @Override
181 public String toString() {
182 final String etn;
183 final String eth;
184 if( null != execThread ) {
185 etn = execThread.getName();
186 eth = "0x"+Integer.toHexString(execThread.hashCode());
187 } else {
188 etn = "n/a";
189 eth = "n/a";
190 }
191 return "RunnableTask[enqueued "+isInQueue()+"[executed "+isExecuted()+", flushed "+isFlushed()+", thread["+eth+", "+etn+"]], tTotal "+getDurationTotal()+" ms, tExec "+getDurationInExec()+" ms, tQueue "+getDurationInQueue()+" ms, attachment "+attachment+", throwable "+getThrowable()+"]";
192 }
193}
194
Helper routines for accessing properties.
static final boolean isPropertyDefined(final String property, final boolean jnlpAlias)
Helper class to provide a Runnable queue implementation with a Runnable wrapper which notifies after ...
Definition: TaskBase.java:39
final Object getSyncObject()
Return the synchronization object if any.
Definition: TaskBase.java:103
final void setAttachment(final Object o)
Attach a custom object to this task.
Definition: TaskBase.java:111
final String getExceptionOutIntro()
Definition: TaskBase.java:82
TaskBase(final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut)
Definition: TaskBase.java:69
final Throwable getThrowable()
Definition: TaskBase.java:171
final Object getAttachment()
Return the attachment object if any.
Definition: TaskBase.java:119
final Thread getExecutionThread()
Returns the execution thread or null if not yet run().
Definition: TaskBase.java:95
final void flush(final Throwable t)
Simply flush this task and notify a waiting executor.
Definition: TaskBase.java:136
volatile boolean isExecuted
Definition: TaskBase.java:57
final PrintStream exceptionOut
Definition: TaskBase.java:50
volatile boolean isFlushed
Definition: TaskBase.java:58