Horus Doc || Java GUI Reference || Doxygen's quick Index  

SwingWorker Class Reference

An abstract class that you subclass to perform GUI-related work in a dedicated thread. More...

List of all members.

Public Methods

abstract Object construct ()
 Compute the value to be returned by the get method. More...

void finished ()
 Called on the event dispatching thread (not on the worker thread) after the construct method has returned. More...

void interrupt ()
 A new method that interrupts the worker thread. More...

Object get ()
 Return the value created by the construct method. More...

 SwingWorker ()
 Start a thread that will call the construct method and then exit. More...


Protected Methods

synchronized Object getValue ()
 Get the value produced by the worker thread, or null if it hasn't been constructed yet. More...


Detailed Description

An abstract class that you subclass to perform GUI-related work in a dedicated thread.

For instructions on using this class, see http://java.sun.com/products/jfc/swingdoc-current/threads2.html


Constructor & Destructor Documentation

SwingWorker::SwingWorker   [inline]
 

Start a thread that will call the construct method and then exit.

00098                      {
00099     final Runnable doFinished = new Runnable() {
00100        public void run() { finished(); }
00101     };
00102 
00103     Runnable doConstruct = new Runnable() {
00104         public void run() {
00105             try {
00106                 //setValue(construct());
00107                 Object obj = construct();
00108                 setValue(obj);
00109             }
00110             finally {
00111                 threadVar.clear();
00112             }
00113 
00114             SwingUtilities.invokeLater(doFinished);
00115         }
00116     };
00117 
00118     Thread t = new Thread(doConstruct);
00119     //t.setPriority(Thread.MAX_PRIORITY);
00120     threadVar = new ThreadVar(t);
00121     t.start();
00122 }


Member Function Documentation

synchronized Object SwingWorker::getValue   [inline, protected]
 

Get the value produced by the worker thread, or null if it hasn't been constructed yet.

00035                                          { 
00036     return value; 
00037 }

abstract Object SwingWorker::construct   [pure virtual]
 

Compute the value to be returned by the get method.

void SwingWorker::finished   [inline]
 

Called on the event dispatching thread (not on the worker thread) after the construct method has returned.

00055                        {
00056 }

void SwingWorker::interrupt   [inline]
 

A new method that interrupts the worker thread.

Call this method to force the worker to abort what it's doing.

00062                         {
00063     Thread t = threadVar.get();
00064     if (t != null) {
00065         t.interrupt();
00066     }
00067     threadVar.clear();
00068 }

Object SwingWorker::get   [inline]
 

Return the value created by the construct method.

Returns null if either the constructing thread or the current thread was interrupted before a value was produced.

Returns:
the value created by the construct method

00077                     {
00078     while (true) {  
00079         Thread t = threadVar.get();
00080         if (t == null) {
00081             return getValue();
00082         }
00083         try {
00084             t.join();
00085         }
00086         catch (InterruptedException e) {
00087             Thread.currentThread().interrupt(); // propagate
00088             return null;
00089         }
00090     }
00091 }


The documentation for this class was generated from the following file:
Generated on Mon Jan 27 15:11:27 2003 for JavaReference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001