|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
wt.util.WTThread
wt.util.EventThread
A thread for processing AWT events asynchronously.
This is an extension of WTThread
that is used to asynchronously process
the semantic level AWT events, ActionEvent
, ItemEvent
,
and TextEvent
.
This class is used as a convenience to reduce the amount of extra code required to perform
processing in a separate thread. This class allows event listeners to perform asynchronous
processing of events by re-executing their event listener method in the context of a new
thread. This avoids the need to create special Thread
or Runnable
classes just to provide customized run
methods.
For example, the following code shows how it might be used from within a
actionPerformed
method to process a particular button's action
in a separate thread.
Theprotected void actionPerformed (ActionEvent event) { Object source = e.getSource(); if (source == goButton) { if (Thread.currentThread() instanceof EventThread) { // This is an asynchronous thread, do the processing ... } else { // Do the work in a new thread new EventThread(this, event).start(null, false); } } else ... }
start
method supports automatically disabling and enabling a target
component as well as allowing the calling thread to wait until the new thread is ready
to let it continue. For example, the following example disables the button and
synchronizes with the new thread to let it access any volatile state before continuing.
Since EventThread
extends WTThread
, listeners for property
change events can be added as well.
protected void processActionEvent (ActionEvent event) { Object source = e.getSource(); if (source == goButton) { Thread thread = Thread.currentThread(); if (thread instanceof EventThread) { // Capture volatile state of other components ... // Add our status component as a listener for thread status changes EventThread event_thread = (EventThread)thread; event_thread.addPropertyChangeListener(statusComponent); // Notify parent thread to continue event_thread.ready(); // Do the processing ... } else { // Do the work in a new thread new EventThread(this, event).start(goButton, true); } } else ... }
Field Summary | |
private Component |
disabledComponent
|
private EventObject |
event
|
private EventListener |
listener
|
private static String |
versionID
|
Fields inherited from class wt.util.WTThread |
CREATED, DONE, PROGRESS_COUNT, PROGRESS_PERCENT, RUNNING, STATE, STATUS |
Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
|
EventThread(ActionListener listener,
ActionEvent event)
Construct a thread to process a ActionEvent . |
protected |
EventThread(EventListener listener,
EventObject event)
Protected constructor used by public constructors or subclasses. |
|
EventThread(ItemListener listener,
ItemEvent event)
Construct a thread to process a ItemEvent . |
|
EventThread(TextListener listener,
TextEvent event)
Construct a thread to process a TextEvent . |
Method Summary | |
protected void |
dispatchEvent()
Dispatch event to listener. |
void |
run()
Run method that dispatches event to listener. |
boolean |
start(Component disable_component,
boolean wait)
Start this thread. |
Methods inherited from class wt.util.WTThread |
addPropertyChangeListener, cancel, currentProgressCount, currentProgressPercent, currentStatus, firePropertyChange, firePropertyChange, getProgressCount, getProgressPercent, getState, getStatus, interrupt, isInterrupted, ready, removePropertyChangeListener, setInterruptHandler, setProgressCount, setProgressPercent, setStatus, start |
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupted, isAlive, isDaemon, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static final String versionID
private EventListener listener
private EventObject event
private Component disabledComponent
Constructor Detail |
protected EventThread(EventListener listener, EventObject event)
listener
- the target EventListener
objectevent
- the EventObject object
public EventThread(ActionListener listener, ActionEvent event)
ActionEvent
.
The new thread becomes a member of the thread group associated with the
WTContext
object for the target ActionListener
component.
The run
method of the new thread invokes the actionPerformed
method on the target ActionListener
.
listener
- the target ActionListener
objectevent
- the ActionEvent object
public EventThread(ItemListener listener, ItemEvent event)
ItemEvent
.
The new thread becomes a member of the thread group associated with the
WTContext
object for the target ItemListener
component.
The run
method of the new thread invokes the itemStateChanged
method on the target ItemListener
.
listener
- the target ItemListener
objectevent
- the ItemEvent object
public EventThread(TextListener listener, TextEvent event)
TextEvent
.
The new thread becomes a member of the thread group associated with the
WTContext
object for the target TextListener
component.
The run
method of the new thread invokes the textValueChanged
method on the target TextListener
.
listener
- the target TextListener
objectevent
- the TextEvent object
Method Detail |
public boolean start(Component disable_component, boolean wait)
Component
object as well
as wait until the new thread calls its ready
method. If a
Component
is disabled, it will automatically be re-enabled when the thread
completes or if there is any exception thrown when trying to start the thread.
This method returns a boolean value to indicate whether the
thread was started successfully or the caller was interrupted while waiting.
disable_component
- a Component
to be disabled (may be null)wait
- calling thread should wait until this thread signals ready
public final void run()
Component
was disabled, it is automatically re-enabled.
Subclasses may extend for new event types by overriding the dispatchEvent
method.
run
in interface Runnable
run
in class WTThread
protected void dispatchEvent()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |