KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SwingWorker


1 import javax.swing.SwingUtilities JavaDoc;
2
3 /**
4  * This is the 3rd version of SwingWorker (also known as
5  * SwingWorker 3), an abstract class that you subclass to
6  * perform GUI-related work in a dedicated thread. For
7  * instructions on and examples of using this class, see:
8  *
9  * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
10  *
11  * Note that the API changed slightly in the 3rd version:
12  * You must now invoke start() on the SwingWorker after
13  * creating it.
14  */

15 public abstract class SwingWorker {
16     private Object JavaDoc value; // see getValue(), setValue()
17

18     /**
19      * Class to maintain reference to current worker thread
20      * under separate synchronization control.
21      */

22     private static class ThreadVar {
23         private Thread JavaDoc thread;
24         ThreadVar(Thread JavaDoc t) { thread = t; }
25         synchronized Thread JavaDoc get() { return thread; }
26         synchronized void clear() { thread = null; }
27     }
28
29     private ThreadVar threadVar;
30
31     /**
32      * Get the value produced by the worker thread, or null if it
33      * hasn't been constructed yet.
34      */

35     protected synchronized Object JavaDoc getValue() {
36         return value;
37     }
38
39     /**
40      * Set the value produced by worker thread
41      */

42     private synchronized void setValue(Object JavaDoc x) {
43         value = x;
44     }
45
46     /**
47      * Compute the value to be returned by the <code>get</code> method.
48      */

49     public abstract Object JavaDoc construct();
50
51     /**
52      * Called on the event dispatching thread (not on the worker thread)
53      * after the <code>construct</code> method has returned.
54      */

55     public void finished() {
56     }
57
58     /**
59      * A new method that interrupts the worker thread. Call this method
60      * to force the worker to stop what it's doing.
61      */

62     public void interrupt() {
63         Thread JavaDoc t = threadVar.get();
64         if (t != null) {
65             t.interrupt();
66         }
67         threadVar.clear();
68     }
69
70     /**
71      * Return the value created by the <code>construct</code> method.
72      * Returns null if either the constructing thread or the current
73      * thread was interrupted before a value was produced.
74      *
75      * @return the value created by the <code>construct</code> method
76      */

77     public Object JavaDoc get() {
78         while (true) {
79             Thread JavaDoc t = threadVar.get();
80             if (t == null) {
81                 return getValue();
82             }
83             try {
84                 t.join();
85             }
86             catch (InterruptedException JavaDoc e) {
87                 Thread.currentThread().interrupt(); // propagate
88
return null;
89             }
90         }
91     }
92
93
94     /**
95      * Start a thread that will call the <code>construct</code> method
96      * and then exit.
97      */

98     public SwingWorker() {
99         final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
100            public void run() { finished(); }
101         };
102
103         Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
104             public void run() {
105                 try {
106                     setValue(construct());
107                 }
108                 finally {
109                     threadVar.clear();
110                 }
111
112                 SwingUtilities.invokeLater(doFinished);
113             }
114         };
115
116         Thread JavaDoc t = new Thread JavaDoc(doConstruct);
117         threadVar = new ThreadVar(t);
118     }
119
120     /**
121      * Start the worker thread.
122      */

123     public void start() {
124         Thread JavaDoc t = threadVar.get();
125         if (t != null) {
126             t.start();
127         }
128     }
129 }
130
Popular Tags