Package de.sciss.common
Class ProcessingThread
- java.lang.Object
-
- de.sciss.common.ProcessingThread
-
- All Implemented Interfaces:
EventManager.Processor,Disposable,java.awt.event.ActionListener,java.lang.Runnable,java.util.EventListener
public class ProcessingThread extends java.lang.Object implements java.lang.Runnable, EventManager.Processor, java.awt.event.ActionListener, Disposable
A subclass of Thread that is capable of dealing with synchronization issues. It will pause all event dispatching related to specified doors which will be locked during processing. It includes helper methods for updating a progress bar and displaying messages and exceptions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classProcessingThread.CancelledExceptionstatic interfaceProcessingThread.Clientstatic classProcessingThread.Eventstatic interfaceProcessingThread.Listener
-
Field Summary
Fields Modifier and Type Field Description protected EventManagerelmprotected java.lang.Exceptionexceptionprotected java.lang.Stringnameprotected floatprogressprotected booleanprogressInvokedprotected intreturnCodeprotected java.lang.Objectsync
-
Constructor Summary
Constructors Constructor Description ProcessingThread(ProcessingThread.Client client, ProgressComponent pc, java.lang.String procName)Creates a new ProcessingThread.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactionPerformed(java.awt.event.ActionEvent e)voidaddListener(ProcessingThread.Listener l)Registers a listener to be notified when the process starts and terminates.voidcancel(boolean doSync)Forwards the cancel request to the client.static ProcessingThreadcurrentThread()voiddispose()static voidflushProgression()java.lang.ObjectgetClientArg(java.lang.Object key)java.util.MapgetClientMap()java.awt.ComponentgetComponent()java.lang.ExceptiongetException()Queries the last exception thrown in the run method.java.lang.StringgetName()intgetReturnCode()Returns the cient's return code.booleanisRunning()voidprocessEvent(BasicEvent e)Processes the next event in the queue.voidputClientArg(java.lang.Object key, java.lang.Object value)voidremoveListener(ProcessingThread.Listener l)Removes a listener from being notified when the process starts and terminates.voidresetProgression()voidrun()The is the main method of a thread; it invokes the client's processRun method.voidsetException(java.lang.Exception e)If the client is capable of catching an exception in its execution block, it should pass it to the pt calling this method.static voidsetNextProgStop(float p)voidsetProgression(float p)Called by the client to update the progress bar.static booleanshouldCancel()voidstart()Starts processing.voidsync()Puts the calling thread in wait mode until the processing is finished.voidsync(int timeout)static voidupdate(float progress)
-
-
-
Field Detail
-
name
protected final java.lang.String name
-
progress
protected volatile float progress
-
progressInvoked
protected volatile boolean progressInvoked
-
exception
protected volatile java.lang.Exception exception
-
elm
protected EventManager elm
-
returnCode
protected int returnCode
-
sync
protected final java.lang.Object sync
-
-
Constructor Detail
-
ProcessingThread
public ProcessingThread(ProcessingThread.Client client, ProgressComponent pc, java.lang.String procName)
Creates a new ProcessingThread. To start the process and starts processing. synchronization: must be called in the event thread- Parameters:
client- Interface whose method runProcessing() is called inside the new thread when it's started.pc- Component responsible for displaying progress bar etc.procName- Name for the thread and the process monitoring
-
-
Method Detail
-
putClientArg
public void putClientArg(java.lang.Object key, java.lang.Object value)
-
getClientArg
public java.lang.Object getClientArg(java.lang.Object key)
-
getClientMap
public java.util.Map getClientMap()
-
getName
public java.lang.String getName()
-
start
public void start()
Starts processing. Call this method only once!
-
sync
public void sync()
Puts the calling thread in wait mode until the processing is finished. This should be called in the event thread only if the process is about to quit very fast since it will otherwise block GUI updates. An example for its use is the prior request to cancel the process. If the amount of time till the end of the process is unknown, the preferred method to wait for the thread is to register a listener!- See Also:
addListener( ProcessingThread.Listener )
-
sync
public void sync(int timeout)
-
cancel
public void cancel(boolean doSync)
Forwards the cancel request to the client.- Parameters:
doSync- iftrue, wait for the client to abort, otherwise return immediately
-
currentThread
public static ProcessingThread currentThread()
-
shouldCancel
public static boolean shouldCancel()
-
update
public static void update(float progress) throws ProcessingThread.CancelledException
-
isRunning
public boolean isRunning()
-
getReturnCode
public int getReturnCode()
Returns the cient's return code.- Returns:
- the return code (
Client.DONEetc.) or-1if the client had not been started
-
addListener
public void addListener(ProcessingThread.Listener l)
Registers a listener to be notified when the process starts and terminates.- Parameters:
l- the listener to register
-
removeListener
public void removeListener(ProcessingThread.Listener l)
Removes a listener from being notified when the process starts and terminates.- Parameters:
l- the listener to unregister
-
dispose
public void dispose()
- Specified by:
disposein interfaceDisposable
-
run
public void run()
The is the main method of a thread; it invokes the client's processRun method.- Specified by:
runin interfacejava.lang.Runnable
-
setException
public void setException(java.lang.Exception e)
If the client is capable of catching an exception in its execution block, it should pass it to the pt calling this method.- Parameters:
e- exception which was thrown in the client's run method. when the thread stops it will display this error to the user.
-
getException
public java.lang.Exception getException()
Queries the last exception thrown in the run method.- Returns:
- the most recent exception or null if no exception was thrown
-
setNextProgStop
public static void setNextProgStop(float p)
-
flushProgression
public static void flushProgression()
-
actionPerformed
public void actionPerformed(java.awt.event.ActionEvent e)
- Specified by:
actionPerformedin interfacejava.awt.event.ActionListener
-
processEvent
public void processEvent(BasicEvent e)
Description copied from interface:EventManager.ProcessorProcesses the next event in the queue. This gets called in the event thread. Usually implementing classes should loop through all listeners by calling elm.countListeners() and elm.getListener(), and invoke specific dispatching methods on these listeners.- Specified by:
processEventin interfaceEventManager.Processor
-
getComponent
public java.awt.Component getComponent()
-
setProgression
public void setProgression(float p)
Called by the client to update the progress bar.- Parameters:
p- new progression between zero and one
-
resetProgression
public void resetProgression()
-
-