KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HsqlTaskQueue


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 /**
35  * Provides very simple queued execution of Runnable objects in a background
36  * thread. The underlying queue is an HsqlDeque instance, an array-based
37  * circular queue implementation with automatic capacity expansion.
38  *
39  * @author boucherb@users
40  * @version 1.7.2
41  * @since 1.7.2
42  */

43 public class HsqlTaskQueue {
44
45     /** The thread used to process commands */
46     protected Thread JavaDoc taskRunnerThread;
47
48     /** Special queue element to signal termination */
49     protected static final Runnable JavaDoc SHUTDOWNTASK = new Runnable JavaDoc() {
50         public void run() {}
51     };
52
53     /**
54      * true if thread should shut down after processing current task.
55      *
56      * Once set true, stays true forever
57      */

58     protected volatile boolean isShutdown;
59
60     public synchronized Thread JavaDoc getTaskRunnerThread() {
61         return taskRunnerThread;
62     }
63
64     protected synchronized void clearThread() {
65         taskRunnerThread = null;
66     }
67
68     protected final HsqlDeque queue = new HsqlDeque();
69
70     protected class TaskRunner implements Runnable JavaDoc {
71
72         public void run() {
73
74             Runnable JavaDoc task;
75
76             try {
77                 while (!isShutdown) {
78                     synchronized (queue) {
79                         task = (Runnable JavaDoc) queue.getFirst();
80                     }
81
82                     if (task == SHUTDOWNTASK) {
83                         isShutdown = true;
84
85                         synchronized (queue) {
86                             queue.clear();
87                         }
88
89                         break;
90                     } else if (task != null) {
91                         task.run();
92
93                         task = null;
94                     } else {
95                         break;
96                     }
97                 }
98             } finally {
99                 clearThread();
100             }
101         }
102     }
103
104     protected final TaskRunner taskRunner = new TaskRunner();
105
106     public HsqlTaskQueue() {}
107
108     public boolean isShutdown() {
109         return isShutdown;
110     }
111
112     public synchronized void restart() {
113
114         if (taskRunnerThread == null &&!isShutdown) {
115             taskRunnerThread = new Thread JavaDoc(taskRunner);
116
117             taskRunnerThread.start();
118         }
119     }
120
121     public void execute(Runnable JavaDoc command) throws RuntimeException JavaDoc {
122
123         if (!isShutdown) {
124             synchronized (queue) {
125                 queue.addLast(command);
126             }
127
128             restart();
129         }
130     }
131
132     public synchronized void shutdownAfterQueued() {
133
134         if (!isShutdown) {
135             synchronized (queue) {
136                 queue.addLast(SHUTDOWNTASK);
137             }
138         }
139     }
140
141     public synchronized void shutdownAfterCurrent() {
142
143         isShutdown = true;
144
145         synchronized (queue) {
146             queue.clear();
147             queue.addLast(SHUTDOWNTASK);
148         }
149     }
150
151     public synchronized void shutdownImmediately() {
152
153         isShutdown = true;
154
155         if (taskRunnerThread != null) {
156             taskRunnerThread.interrupt();
157         }
158
159         synchronized (queue) {
160             queue.clear();
161             queue.addLast(SHUTDOWNTASK);
162         }
163     }
164 }
165
Popular Tags