KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > swing > SwingWorker


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

17 public abstract class SwingWorker {
18     private Object JavaDoc value;
19
20     /**
21      * Class to maintain reference to current worker thread
22      * under separate synchronization control.
23      */

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

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

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

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

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

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

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

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

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