jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TaskBase.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2010 Gothel Software e.K.
5 * Copyright (c) 2010 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package org.jau.util.parallel;
28
29import java.io.PrintStream;
30
31import org.jau.sys.Debug;
32import org.jau.sys.PropertyAccess;
33
34/**
35 * Helper class to provide a Runnable queue implementation with a Runnable wrapper
36 * which notifies after execution for the <code>invokeAndWait()</code> semantics.
37 */
38public abstract class TaskBase implements Runnable {
39 /** Enable via the property <code>jogamp.debug.TaskBase.TraceSource</code> */
40 private static final boolean TRACE_SOURCE;
41
42 static {
44 TRACE_SOURCE = PropertyAccess.isPropertyDefined("jau.debug.TaskBase.TraceSource", true);
45 }
46
47 protected final Object syncObject;
48 protected final boolean catchExceptions;
49 protected final PrintStream exceptionOut;
50 protected final Throwable sourceStack;
51
52 protected Object attachment;
53 protected Throwable runnableException;
54 protected long tCreated, tStarted;
55 protected volatile long tExecuted;
56 protected volatile boolean isExecuted;
57 protected volatile boolean isFlushed;
58 protected volatile Thread execThread;
59
60 /**
61 * @param syncObject The synchronization object if caller wait until <code>runnable</code> execution is completed,
62 * or <code>null</code> if waiting is not desired.
63 * @param catchExceptions Influence an occurring exception during <code>runnable</code> execution.
64 * If <code>true</code>, the exception is silenced and can be retrieved via {@link #getThrowable()},
65 * otherwise the exception is thrown.
66 * @param exceptionOut If not <code>null</code>, exceptions are written to this {@link PrintStream}.
67 */
68 protected TaskBase(final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut) {
69 this.syncObject = syncObject;
70 this.catchExceptions = catchExceptions;
71 this.exceptionOut = exceptionOut;
72 this.sourceStack = TRACE_SOURCE ? new Throwable("Creation @") : null;
73 this.tCreated = System.currentTimeMillis();
74 this.tStarted = 0;
75 this.tExecuted = 0;
76 this.isExecuted = false;
77 this.isFlushed = false;
78 this.execThread = null;
79 }
80
81 protected final String getExceptionOutIntro() {
82 return catchExceptions ? "A caught" : "An uncaught";
83 }
84 protected final void printSourceTrace() {
85 if( null != sourceStack && null != exceptionOut ) {
86 sourceStack.printStackTrace(exceptionOut);
87 }
88 }
89
90 /**
91 * Returns the execution thread or {@code null} if not yet {@link #run()}.
92 * @since 0.3.0
93 */
94 public final Thread getExecutionThread() {
95 return execThread;
96 }
97
98 /**
99 * Return the synchronization object if any.
100 * @see #RunnableTask(Runnable, Object, boolean)
101 */
102 public final Object getSyncObject() {
103 return syncObject;
104 }
105
106 /**
107 * Attach a custom object to this task.
108 * Useful to piggybag further information, ie tag a task final.
109 */
110 public final void setAttachment(final Object o) {
111 attachment = o;
112 }
113
114 /**
115 * Return the attachment object if any.
116 * @see #setAttachment(Object)
117 */
118 public final Object getAttachment() {
119 return attachment;
120 }
121
122 @Override
123 public abstract void run();
124
125 /**
126 * Simply flush this task and notify a waiting executor.
127 * The executor which might have been blocked until notified
128 * will be unblocked and the task removed from the queue.
129 *
130 * @param t optional Throwable to be assigned for later {@link #getThrowable()} query in case of an error.
131 *
132 * @see #isFlushed()
133 * @see #isInQueue()
134 */
135 public final void flush(final Throwable t) {
136 if(!isExecuted() && hasWaiter()) {
138 synchronized (syncObject) {
139 isFlushed = true;
140 syncObject.notifyAll();
141 }
142 }
143 }
144
145 /**
146 * @return !{@link #isExecuted()} && !{@link #isFlushed()}
147 */
148 public final boolean isInQueue() { return !isExecuted && !isFlushed; }
149
150 /**
151 * @return True if executed, otherwise false;
152 */
153 public final boolean isExecuted() { return isExecuted; }
154
155 /**
156 * @return True if flushed, otherwise false;
157 */
158 public final boolean isFlushed() { return isFlushed; }
159
160 /**
161 * @return True if invoking thread waits until done,
162 * ie a <code>notifyObject</code> was passed, otherwise false;
163 */
164 public final boolean hasWaiter() { return null != syncObject; }
165
166 /**
167 * @return A thrown exception while execution of the user action, if any and if caught
168 * @see #RunnableTask(Runnable, Object, boolean)
169 */
170 public final Throwable getThrowable() { return runnableException; }
171
172 public final long getTimestampCreate() { return tCreated; }
173 public final long getTimestampBeforeExec() { return tStarted; }
174 public final long getTimestampAfterExec() { return tExecuted; }
175 public final long getDurationInQueue() { return tStarted - tCreated; }
176 public final long getDurationInExec() { return 0 < tExecuted ? tExecuted - tStarted : 0; }
177 public final long getDurationTotal() { return 0 < tExecuted ? tExecuted - tCreated : tStarted - tCreated; }
178
179 @Override
180 public String toString() {
181 final String etn;
182 final String eth;
183 if( null != execThread ) {
184 etn = execThread.getName();
185 eth = "0x"+Integer.toHexString(execThread.hashCode());
186 } else {
187 etn = "n/a";
188 eth = "n/a";
189 }
190 return "RunnableTask[enqueued "+isInQueue()+"[executed "+isExecuted()+", flushed "+isFlushed()+", thread["+eth+", "+etn+"]], tTotal "+getDurationTotal()+" ms, tExec "+getDurationInExec()+" ms, tQueue "+getDurationInQueue()+" ms, attachment "+attachment+", throwable "+getThrowable()+"]";
191 }
192}
193
Helper routines for logging and debugging.
Definition: Debug.java:35
static final void initSingleton()
Ensures static init block has been issues, i.e.
Definition: Debug.java:53
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:38
final void setAttachment(final Object o)
Attach a custom object to this task.
Definition: TaskBase.java:110
final Throwable getThrowable()
Definition: TaskBase.java:170
volatile boolean isFlushed
Definition: TaskBase.java:57
final Thread getExecutionThread()
Returns the execution thread or null if not yet run().
Definition: TaskBase.java:94
final long getTimestampBeforeExec()
Definition: TaskBase.java:173
final String getExceptionOutIntro()
Definition: TaskBase.java:81
TaskBase(final Object syncObject, final boolean catchExceptions, final PrintStream exceptionOut)
Definition: TaskBase.java:68
volatile Thread execThread
Definition: TaskBase.java:58
final void flush(final Throwable t)
Simply flush this task and notify a waiting executor.
Definition: TaskBase.java:135
final Object getAttachment()
Return the attachment object if any.
Definition: TaskBase.java:118
final long getTimestampAfterExec()
Definition: TaskBase.java:174
volatile boolean isExecuted
Definition: TaskBase.java:56
final Throwable sourceStack
Definition: TaskBase.java:50
final Object getSyncObject()
Return the synchronization object if any.
Definition: TaskBase.java:102
final PrintStream exceptionOut
Definition: TaskBase.java:49