KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > utils > SwingWorker


1 /*
2  * $Id: SwingWorker.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

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

24 public abstract class SwingWorker {
25     private Object JavaDoc value; // see getValue(), setValue()
26
private Thread JavaDoc thread;
27
28     /**
29      * Class to maintain reference to current worker thread
30      * under separate synchronization control.
31      */

32     private static class ThreadVar {
33         private Thread JavaDoc thread;
34         ThreadVar(Thread JavaDoc t) { thread = t; }
35         synchronized Thread JavaDoc get() { return thread; }
36         synchronized void clear() { thread = null; }
37     }
38
39     private ThreadVar threadVar;
40
41     /**
42      * Get the value produced by the worker thread, or null if it
43      * hasn't been constructed yet.
44      */

45     protected synchronized Object JavaDoc getValue() {
46         return value;
47     }
48
49     /**
50      * Set the value produced by worker thread
51      */

52     private synchronized void setValue(Object JavaDoc x) {
53         value = x;
54     }
55
56     /**
57      * Compute the value to be returned by the <code>get</code> method.
58      */

59     public abstract Object JavaDoc construct();
60
61     /**
62      * Called on the event dispatching thread (not on the worker thread)
63      * after the <code>construct</code> method has returned.
64      */

65     public void finished() {
66     }
67
68     /**
69      * A new method that interrupts the worker thread. Call this method
70      * to force the worker to stop what it's doing.
71      */

72     public void interrupt() {
73         Thread JavaDoc t = threadVar.get();
74         if (t != null) {
75             t.interrupt();
76         }
77         threadVar.clear();
78     }
79
80     /**
81      * Return the value created by the <code>construct</code> method.
82      * Returns null if either the constructing thread or the current
83      * thread was interrupted before a value was produced.
84      *
85      * @return the value created by the <code>construct</code> method
86      */

87     public Object JavaDoc get() {
88         while (true) {
89             Thread JavaDoc t = threadVar.get();
90             if (t == null) {
91                 return getValue();
92             }
93             try {
94                 t.join();
95             }
96             catch (InterruptedException JavaDoc e) {
97                 Thread.currentThread().interrupt(); // propagate
98
return null;
99             }
100         }
101     }
102
103
104     /**
105      * Start a thread that will call the <code>construct</code> method
106      * and then exit.
107      */

108     public SwingWorker() {
109         final Runnable JavaDoc doFinished = new Runnable JavaDoc() {
110            public void run() { finished(); }
111         };
112
113         Runnable JavaDoc doConstruct = new Runnable JavaDoc() {
114             public void run() {
115                 try {
116                     setValue(construct());
117                 }
118                 finally {
119                     threadVar.clear();
120                 }
121
122                 SwingUtilities.invokeLater(doFinished);
123             }
124         };
125
126         Thread JavaDoc t = new Thread JavaDoc(doConstruct);
127         threadVar = new ThreadVar(t);
128     }
129
130     /**
131      * Start the worker thread.
132      */

133     public void start() {
134         Thread JavaDoc t = threadVar.get();
135         if (t != null) {
136             t.start();
137         }
138     }
139 }
140
Popular Tags