KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > ConcurrentWorker


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 foxtrot.workers.MultiWorkerThread;
12
13 /**
14  * The class that execute time-consuming {@link Task}s and {@link Job}s, but differently
15  * from {@link Worker}, execute them concurrently and not one after the other. <br />
16  * Most of the times, the best choice for having synchronous behavior is {@link Worker},
17  * so please think twice before using this class. <br />
18  * A typical usage of this class is, for example, in an application with multiple tabs
19  * where the user triggers the loading of the content of the tab by clicking on it,
20  * and where tab loading is time-consuming; in such scenario, the behavior of
21  * {@link Worker} would be to load the first tab and only when the first tab is loaded
22  * start loading the second tab.
23  * The behavior of ConcurrentWorker would be to load the tabs concurrently. <br />
24  * Needless to say, this concurrent behavior imposes further care in designing the
25  * system to respect thread safety, and to keep the system simple to understand. <br />
26  * This approach is very similar to the asynchronous behavior, but with the advantages
27  * of the synchronous model in code readability.
28  */

29 public class ConcurrentWorker extends AbstractSyncWorker
30 {
31    private static ConcurrentWorker instance = new ConcurrentWorker();
32
33    /**
34     * Cannot be instantiated, use static methods only.
35     */

36    private ConcurrentWorker()
37    {
38    }
39
40    /**
41     * @see Worker#getWorkerThread
42     */

43    public static WorkerThread getWorkerThread()
44    {
45       return instance.workerThread();
46    }
47
48    /**
49     * @see Worker#setWorkerThread
50     */

51    public static void setWorkerThread(WorkerThread workerThread)
52    {
53       instance.workerThread(workerThread);
54    }
55
56    WorkerThread createDefaultWorkerThread()
57    {
58       return new MultiWorkerThread();
59    }
60
61    /**
62     * @see Worker#getEventPump()
63     */

64    public static EventPump getEventPump()
65    {
66       return instance.eventPump();
67    }
68
69    /**
70     * @see Worker#setEventPump(EventPump)
71     */

72    public static void setEventPump(EventPump eventPump)
73    {
74       instance.eventPump(eventPump);
75    }
76
77    /**
78     * @see Worker#post(Task)
79     * @see #post(Job)
80     */

81    public static Object JavaDoc post(Task task) throws Exception JavaDoc
82    {
83       return instance.post(task, getWorkerThread(), getEventPump());
84    }
85
86    /**
87     * @see Worker#post(Job)
88     * @see #post(Task)
89     */

90    public static Object JavaDoc post(Job job) throws Exception JavaDoc
91    {
92       return instance.post(job, getWorkerThread(), getEventPump());
93    }
94 }
95
Popular Tags