|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--de.hunsicker.util.concurrent.ThreadFactoryUser | +--de.hunsicker.swing.util.SwingWorker
An abstract class that you subclass to perform GUI-related work in a dedicated thread.
This class was adapted from the SwingWorker written by Hans Muller and presented in "Using a Swing Worker Thread" in the Swing Connection - http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
A closely related version of this class is described in "The Last Word in Swing Threads" in the Swing Connection - http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html
This SwingWorker is a ThreadFactoryUser and implements Runnable. The default thread factory creates low-priority worker threads. A special constructor is provided for enabling a timeout. When the timeout expires, the worker thread is interrupted.
Note: Using a timeout of Long.MAX_VALUE
will not impose a timeout but will
create an additional thread of control that will respond to an interrupt even if the
construct()
implementation ignores them.
Sample Usage
import EDU.oswego.cs.dl.util.concurrent.TimeoutException; import EDU.oswego.cs.dl.util.concurrent.misc.SwingWorker; public class SwingWorkerDemo extends javax.swing.JApplet { private static final int TIMEOUT = 5000; // 5 seconds private javax.swing.JButton start; private javax.swing.JLabel status; private SwingWorker worker; public SwingWorkerDemo() { status = new javax.swing.JLabel("Ready"); status.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); getContentPane().add(status, java.awt.BorderLayout.CENTER); start = new javax.swing.JButton("Start"); getContentPane().add(start, java.awt.BorderLayout.SOUTH); start.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { if (start.getText().equals("Start")) { start.setText("Stop"); status.setText("Working..."); worker = new DemoSwingWorker(TIMEOUT); worker.start(); } else { worker.interrupt(); } } }); } private class DemoSwingWorker extends SwingWorker { private static final java.util.Random RAND = new java.util.Random(); public DemoSwingWorker(long msecs) { super(msecs); } protected Object construct() throws InterruptedException { // Take a random nap. If we oversleep, the worker times out. Thread.sleep(RAND.nextInt(2 * TIMEOUT)); return "Success"; } protected void finished() { start.setText("Start"); try { Object result = get(); status.setText((String)result); } catch (java.lang.reflect.InvocationTargetException e) { Throwable ex = e.getTargetException(); if (ex instanceof TimeoutException) { status.setText("Timed out."); } else if (ex instanceof InterruptedException) { status.setText("Interrupted."); } else { status.setText("Exception: " + ex); } } catch (InterruptedException ex) { // event-dispatch thread won't be interrupted throw new IllegalStateException(ex + ""); } } } }
Nested Class Summary |
Nested classes inherited from class de.hunsicker.util.concurrent.ThreadFactoryUser |
ThreadFactoryUser.DefaultThreadFactory |
Field Summary |
Fields inherited from class de.hunsicker.util.concurrent.ThreadFactoryUser |
threadFactory_ |
Constructor Summary | |
|
SwingWorker()
Creates new SwingWorker with no timeout. |
|
SwingWorker(long msecs)
Creates new SwingWorker with specified timeout. |
protected |
SwingWorker(ThreadFactory factory,
long msecs)
Creates new SwingWorker with specified thread factory and timeout. |
Method Summary | |
protected abstract java.lang.Object |
construct()
Computes the value to be returned by the get() method. |
protected void |
finished()
Called on the event dispatching thread (not on the worker thread) after the construct() method has returned. |
java.lang.Object |
get()
Return the value created by the construct() method, waiting if necessary
until it is ready. |
java.lang.reflect.InvocationTargetException |
getException()
Get the exception, or null if there isn't one (yet). |
long |
getTimeout()
Returns timeout period in milliseconds. |
void |
interrupt()
Stops the worker and sets the exception to InterruptedException. |
boolean |
isReady()
Return whether the get() method is ready to return a value. |
void |
run()
Calls the construct() method to compute the result, and then invokes the
finished() method on the event dispatch thread. |
void |
start()
Starts the worker thread. |
java.lang.Object |
timedGet(long msecs)
Wait at most msecs to access the constructed result. |
Methods inherited from class de.hunsicker.util.concurrent.ThreadFactoryUser |
getThreadFactory, setThreadFactory |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public SwingWorker()
public SwingWorker(long msecs)
msecs
- timeout in milliseconds, or 0
for no time limit.protected SwingWorker(ThreadFactory factory, long msecs)
factory
- factory for worker threads.msecs
- timeout in milliseconds, or 0
for no time limit.
java.lang.IllegalArgumentException
- DOCUMENT ME!Method Detail |
public java.lang.reflect.InvocationTargetException getException()
null
if there isn't one (yet). This does not
wait until the worker is ready, so should ordinarily only be called if you know
it is.
construct()
method wrapped in an
InvocationTargetException.public boolean isReady()
get()
method is ready to return a value.
public long getTimeout()
0
(default).
public java.lang.Object get() throws java.lang.InterruptedException, java.lang.reflect.InvocationTargetException
construct()
method, waiting if necessary
until it is ready.
construct()
method
java.lang.InterruptedException
- if current thread was interrupted
java.lang.reflect.InvocationTargetException
- if the constructing thread encountered an
exception or was interrupted.public void interrupt()
public void run()
construct()
method to compute the result, and then invokes the
finished()
method on the event dispatch thread.
run
in interface java.lang.Runnable
public void start()
public java.lang.Object timedGet(long msecs) throws TimeoutException, java.lang.InterruptedException, java.lang.reflect.InvocationTargetException
msecs
- DOCUMENT ME!
TimeoutException
- if not ready after msecs
java.lang.InterruptedException
- if current thread has been interrupted
java.lang.reflect.InvocationTargetException
- if the constructing thread encountered an
exception or was interrupted.protected abstract java.lang.Object construct() throws java.lang.Exception
get()
method.
java.lang.Exception
- DOCUMENT ME!protected void finished()
construct()
method has returned.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |