File: SwingWorker.java

package info (click to toggle)
concurrent-dfsg 1.3.4-6
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 976 kB
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (286 lines) | stat: -rw-r--r-- 9,517 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
  File: SwingWorker.java

  Originally written by Joseph Bowbeer and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
 
  Originally part of jozart.swingutils.
  Adapted for util.concurrent by Joseph Bowbeer.

*/

package EDU.oswego.cs.dl.util.concurrent.misc;

import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;

import EDU.oswego.cs.dl.util.concurrent.*;

/**
 * An abstract class that you subclass to perform GUI-related work
 * in a dedicated thread.
 * <p>
 * This class was adapted from the SwingWorker written by Hans Muller
 * and presented in "Using a Swing Worker Thread" in the Swing Connection
 * - http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
 * <p>
 * A closely related version of this class is described in
 * "The Last Word in Swing Threads" in the Swing Connection
 * - http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html
 * <p>
 * This SwingWorker is a ThreadFactoryUser and implements Runnable. The
 * default thread factory creates low-priority worker threads. A special
 * constructor is provided for enabling a timeout. When the timeout
 * expires, the worker thread is interrupted.
 * <p>
 * Note: Using a timeout of <code>Long.MAX_VALUE</code> will not impose a
 * timeout but will create an additional thread of control that will respond
 * to an interrupt even if the <code>construct</code> implementation ignores
 * them.
 * <p>
 * <b>Sample Usage</b> <p>
 * <pre>
 * import EDU.oswego.cs.dl.util.concurrent.TimeoutException;
 * import EDU.oswego.cs.dl.util.concurrent.misc.SwingWorker;
 *
 * public class SwingWorkerDemo extends javax.swing.JApplet {
 *
 *   private static final int TIMEOUT = 5000; // 5 seconds
 *   private javax.swing.JLabel status;
 *   private javax.swing.JButton start;
 *   private SwingWorker worker;
 *
 *   public SwingWorkerDemo() {
 *     status = new javax.swing.JLabel("Ready");
 *     status.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
 *     getContentPane().add(status, java.awt.BorderLayout.CENTER);
 *     start = new javax.swing.JButton("Start");
 *     getContentPane().add(start, java.awt.BorderLayout.SOUTH);
 *
 *     start.addActionListener(new java.awt.event.ActionListener() {
 *       public void actionPerformed(java.awt.event.ActionEvent evt) {
 *         if (start.getText().equals("Start")) {
 *           start.setText("Stop");
 *           status.setText("Working...");
 *           worker = new DemoSwingWorker(TIMEOUT);
 *           worker.start();
 *         } else {
 *           worker.interrupt();
 *         }
 *       }
 *     });
 *   }
 *
 *   private class DemoSwingWorker extends SwingWorker {
 *     private static final java.util.Random RAND = new java.util.Random();
 *     public DemoSwingWorker(long msecs) {
 *       super(msecs);
 *     }
 *     protected Object construct() throws InterruptedException {
 *       // Take a random nap. If we oversleep, the worker times out.
 *       Thread.sleep(RAND.nextInt(2*TIMEOUT));
 *       return "Success";
 *     }
 *     protected void finished() {
 *       start.setText("Start");
 *       try {
 *         Object result = get();
 *         status.setText((String) result);
 *       }
 *       catch (java.lang.reflect.InvocationTargetException e) {
 *         Throwable ex = e.getTargetException();
 *         if (ex instanceof TimeoutException) {
 *           status.setText("Timed out.");
 *         } else if (ex instanceof InterruptedException) {
 *           status.setText("Interrupted.");
 *         } else {
 *           status.setText("Exception: " + ex);
 *         }
 *       }
 *       catch (InterruptedException ex) {
 *         // event-dispatch thread won't be interrupted 
 *         throw new IllegalStateException(ex+"");
 *       }
 *     }
 *   }
 * }
 * </pre>
 *
 * @author  Joseph Bowbeer
 * @author  Hans Muller
 * @version 3.0
 *
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
 */
public abstract class SwingWorker extends ThreadFactoryUser
implements Runnable {

    /** Default thread factory. Creates low priority worker threads. */
    private static final ThreadFactory FACTORY = new ThreadFactory() {
        public Thread newThread(Runnable command) {
            Thread t = new Thread(command);
            t.setPriority(Thread.MIN_PRIORITY+1);
            return t;
        }
    };

    /** Holds the value to be returned by the <code>get</code> method. */
    private final FutureResult result = new FutureResult();

    /** Maximum time to wait for worker to complete. */
    private final long timeout;
    
    /** Worker thread. */
    private Thread thread;

    /** Creates new SwingWorker with no timeout. */
    public SwingWorker() {
        this(FACTORY, 0);
    }
    
    /**
     * Creates new SwingWorker with specified timeout.
     * @param msecs timeout in milliseconds, or <code>0</code>
     * for no time limit.
     */
    public SwingWorker(long msecs) {
        this(FACTORY, msecs);
    }
    
    /**
     * Creates new SwingWorker with specified thread factory and timeout.
     * @param factory factory for worker threads.
     * @param msecs timeout in milliseconds, or <code>0</code>
     * for no time limit.
     */
    protected SwingWorker(ThreadFactory factory, long msecs) {
        setThreadFactory(factory);
        if (msecs < 0) {
            throw new IllegalArgumentException("timeout="+msecs);
        }
        timeout = msecs;
    }

    /**
     * Computes the value to be returned by the <code>get</code> method.
     */
    protected abstract Object construct() throws Exception;

    /**
     * Called on the event dispatching thread (not on the worker thread)
     * after the <code>construct</code> method has returned.
     */
    protected void finished() { }

    /**
     * Returns timeout period in milliseconds. Timeout is the
     * maximum time to wait for worker to complete. There is
     * no time limit if timeout is <code>0</code> (default).
     */
    public long getTimeout() {
        return timeout;
    }

    /**
     * Calls the <code>construct</code> method to compute the result,
     * and then invokes the <code>finished</code> method on the event
     * dispatch thread.
     */
    public void run() {

        Callable function = new Callable() {
            public Object call() throws Exception {
                return construct();
            }
        };

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

        /* Convert to TimedCallable if timeout is specified. */
        long msecs = getTimeout();
        if (msecs != 0) {
            TimedCallable tc = new TimedCallable(function, msecs);
            tc.setThreadFactory(getThreadFactory());
            function = tc;
        }

        result.setter(function).run();
        SwingUtilities.invokeLater(doFinished);
    }

    /**
     * Starts the worker thread.
     */
    public synchronized void start() {
        if (thread == null) {
            thread = getThreadFactory().newThread(this);
        }
        thread.start();
    }

    /**
     * Stops the worker and sets the exception to InterruptedException.
     */
    public synchronized void interrupt() {
        if (thread != null) {
            /* Try-catch is workaround for JDK1.2 applet security bug.
               On some platforms, a security exception is thrown if an
               applet interrupts a thread that is no longer alive. */
            try { thread.interrupt(); } catch (Exception ex) { }
        }
        result.setException(new InterruptedException());
    }

    /**
     * Return the value created by the <code>construct</code> method,
     * waiting if necessary until it is ready.
     *
     * @return the value created by the <code>construct</code> method
     * @exception InterruptedException if current thread was interrupted
     * @exception InvocationTargetException if the constructing thread
     * encountered an exception or was interrupted.
     */
    public Object get()
    throws InterruptedException, InvocationTargetException {
        return result.get();
    }

    /**
     * Wait at most msecs to access the constructed result.
     * @return current value
     * @exception TimeoutException if not ready after msecs
     * @exception InterruptedException if current thread has been interrupted
     * @exception InvocationTargetException if the constructing thread
     * encountered an exception or was interrupted.
     */
    public Object timedGet(long msecs) 
    throws TimeoutException, InterruptedException, InvocationTargetException {
        return result.timedGet(msecs);
    }

    /**
     * Get the exception, or null if there isn't one (yet).
     * This does not wait until the worker is ready, so should
     * ordinarily only be called if you know it is.
     * @return the exception encountered by the <code>construct</code>
     * method wrapped in an InvocationTargetException
     */
    public InvocationTargetException getException() {
        return result.getException();
    }

    /**
     * Return whether the <code>get</code> method is ready to
     * return a value.
     *
     * @return true if a value or exception has been set. else false
     */
    public boolean isReady() {
        return result.isReady();
    }

}