root/trunk/LogicMail/src/org/logicprobe/LogicMail/util/ThreadQueue.java

Revision 694, 4.4 KB (checked in by octorian, 2 weeks ago)

Removed lingering calls to System.err.println()

  • Property svn:mime-type set to text/plain
Line 
1/*-
2 * Copyright (c) 2008, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31package org.logicprobe.LogicMail.util;
32
33import net.rim.device.api.system.EventLogger;
34
35import org.logicprobe.LogicMail.AppInfo;
36
37// TODO: Write rigorous tests for this class
38
39/**
40 * Provides a work item queue for <tt>Runnable</tt> objects.
41 * This is similar to a thread pool, except all work items
42 * run in sequence.  Also, the thread is not kept alive
43 * when there are no pending work items.
44 */
45public class ThreadQueue {
46        private Queue runnableQueue;
47        private ThreadQueueThread threadQueueThread;
48        private boolean isShutdown;
49       
50        /**
51         * Instantiates a new thread queue.
52         */
53        public ThreadQueue() {
54                runnableQueue = new Queue();
55        }
56       
57        /**
58         * Flushes any pending work items, and optionally
59         * waits for the thread to join.
60         *
61         * @param wait True to wait for the thread to join.
62         */
63        public void shutdown(boolean wait) {
64                isShutdown = true;
65                synchronized(runnableQueue) {
66                        runnableQueue.clear();
67                }
68                if(wait && threadQueueThread != null) {
69                        try {
70                                threadQueueThread.join();
71                        } catch (InterruptedException e) { }
72                        threadQueueThread = null;
73                }
74        }
75       
76        /**
77         * Puts the provided <tt>Runnable</tt> object on the
78         * work item queue.  Starts the worker thread if necessary.
79         *
80         * @param runnable The <tt>Runnable</tt> object.
81         * @throws IllegalStateException Thrown if {@link #shutdown(boolean)} has been called.
82         */
83        public void invokeLater(Runnable runnable) {
84                if(isShutdown) {
85                        throw new IllegalStateException("Thread queue has been shutdown");
86                }
87                boolean queued = false;
88                synchronized(runnableQueue) {
89                        if(threadQueueThread != null && threadQueueThread.isAlive()) {
90                                runnableQueue.add(runnable);
91                                queued = true;
92                        }
93                }
94                if(!queued) {
95                        if(threadQueueThread != null) {
96                                try {
97                                        threadQueueThread.join();
98                                } catch (InterruptedException e) { }
99                                threadQueueThread = null;
100                        }
101                        threadQueueThread = new ThreadQueueThread();
102                        runnableQueue.add(runnable);
103                        threadQueueThread.start();
104                }
105        }
106       
107        /**
108         * Actual thread implementation used for the work item queue.
109         */
110        private class ThreadQueueThread extends Thread {
111                /**
112                 * Instantiates a new thread queue thread.
113                 */
114                public ThreadQueueThread() {
115                }
116
117                /* (non-Javadoc)
118                 * @see java.lang.Thread#run()
119                 */
120                public void run() {
121                        while(true) {
122                                Runnable runnable;
123                                synchronized(runnableQueue) {
124                                        if(runnableQueue.element() != null) {
125                                                runnable = (Runnable)runnableQueue.remove();
126                                        }
127                                        else {
128                                                return;
129                                        }
130                                }
131                                Thread.yield();
132                                try {
133                                        runnable.run();
134                                } catch (RuntimeException exp) {
135                        EventLogger.logEvent(AppInfo.GUID,
136                                ("RuntimeException: " + exp.getMessage()).getBytes(),
137                                EventLogger.ERROR);
138                                }
139                        }
140                }
141        }
142}
Note: See TracBrowser for help on using the browser.