KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > AsyncWorker


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot;
10
11 import javax.swing.SwingUtilities JavaDoc;
12
13 import foxtrot.workers.MultiWorkerThread;
14
15 /**
16  * The class that executes {@link AsyncTask asynchronous tasks}. <br />
17  * This class is used when asynchronous task processing is needed, while {@link Worker}
18  * is normally used for synchronous task processing.
19  * This class offers a functionality similar to what provided by
20  * <a HREF="http://java.sun.com/products/jfc/tsc/articles/threads/update.html">
21  * Sun's SwingWorker
22  * </a>.
23  * Example usage:
24  * <pre>
25  * final JButton button = new JButton("Send a message !");
26  * button.addActionListener(new ActionListener()
27  * {
28  * public void actionPerformed(ActionEvent e)
29  * {
30  * AsyncWorker.post(new AsyncTask()
31  * {
32  * public Object run() throws Exception
33  * {
34  * // Send some long message
35  * Thread.sleep(10000);
36  * return null;
37  * }
38  *
39  * public void finish()
40  * {
41  * // Check to see if there are exceptions
42  * // by calling getResultOrThrow()
43  * try
44  * {
45  * getResultOrThrow();
46  * button.setText("Message sent !");
47  * }
48  * catch (Exception x)
49  * {
50  * // Report exception to the user, or just log it
51  * }
52  * }
53  * });
54  * }
55  * });
56  * </pre>
57  * @version $Revision: 1.2 $
58  */

59 public class AsyncWorker extends AbstractWorker
60 {
61    private static AsyncWorker instance = new AsyncWorker();
62
63    /**
64     * Cannot be instantiated, use static methods only.
65     */

66    private AsyncWorker()
67    {
68    }
69
70    /**
71     * @see Worker#getWorkerThread
72     */

73    public static WorkerThread getWorkerThread()
74    {
75       return instance.workerThread();
76    }
77
78    /**
79     * @see Worker#setWorkerThread
80     */

81    public static void setWorkerThread(WorkerThread workerThread)
82    {
83       instance.workerThread(workerThread);
84    }
85
86    /**
87     * Creates and returns the default WorkerThread for this worker
88     */

89    WorkerThread createDefaultWorkerThread()
90    {
91       return new MultiWorkerThread();
92    }
93
94    /**
95     * Executes asynchronously the given AsyncTask in a worker thread. <br />
96     * This method returns immediately; when the AsyncTask is finished,
97     * its {@link AsyncTask#finish()} method will be called in the Event Dispatch
98     * Thread.
99     *
100     * @param task
101     */

102    public static void post(AsyncTask task)
103    {
104       instance.post(task, getWorkerThread());
105    }
106
107    private void post(AsyncTask task, WorkerThread workerThread)
108    {
109       boolean isEventThread = SwingUtilities.isEventDispatchThread();
110       if (!isEventThread)
111       {
112          throw new IllegalStateException JavaDoc("AsyncWorker.post() can be called only from the AWT Event Dispatch Thread");
113       }
114       workerThread.postTask(task);
115    }
116 }
117
Popular Tags