FileDocCategorySizeDatePackage
SwingWorker.javaAPI DocExample3462Tue Dec 12 18:59:20 GMT 2000None

SwingWorker

public abstract class SwingWorker extends Object
This is the 3rd version of SwingWorker (also known as SwingWorker 3), 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/docs/books/tutorial/uiswing/misc/threads.html Note that the API changed slightly in the 3rd version: You must now invoke start() on the SwingWorker after creating it.

Fields Summary
private Object
value
private Thread
thread
private ThreadVar
threadVar
Constructors Summary
public SwingWorker()
Start a thread that will call the construct method and then exit.

        final Runnable doFinished = new Runnable() {
           public void run() { finished(); }
        };

        Runnable doConstruct = new Runnable() { 
            public void run() {
                try {
                    setValue(construct());
                }
                finally {
                    threadVar.clear();
                }

                SwingUtilities.invokeLater(doFinished);
            }
        };

        Thread t = new Thread(doConstruct);
        threadVar = new ThreadVar(t);
    
Methods Summary
public abstract java.lang.Objectconstruct()
Compute the value to be returned by the get method.

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

    
public java.lang.Objectget()
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.

return
the value created by the construct method

        while (true) {  
            Thread t = threadVar.get();
            if (t == null) {
                return getValue();
            }
            try {
                t.join();
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // propagate
                return null;
            }
        }
    
protected synchronized java.lang.ObjectgetValue()
Get the value produced by the worker thread, or null if it hasn't been constructed yet.

 
        return value; 
    
public voidinterrupt()
A new method that interrupts the worker thread. Call this method to force the worker to stop what it's doing.

        Thread t = threadVar.get();
        if (t != null) {
            t.interrupt();
        }
        threadVar.clear();
    
private synchronized voidsetValue(java.lang.Object x)
Set the value produced by worker thread

 
        value = x; 
    
public voidstart()
Start the worker thread.

        Thread t = threadVar.get();
        if (t != null) {
            t.start();
        }