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

Revision 330, 4.1 kB (checked in by octorian, 7 weeks ago)

Thread queue improvements

  • 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
33// TODO: Write rigorous tests for this class
34
35/**
36 * Provides a work item queue for <tt>Runnable</tt> objects.
37 * This is similar to a thread pool, except all work items
38 * run in sequence.  Also, the thread is not kept alive
39 * when there are no pending work items.
40 */
41public class ThreadQueue {
42        private Queue runnableQueue;
43        private ThreadQueueThread threadQueueThread;
44        private boolean isShutdown;
45       
46        /**
47         * Instantiates a new thread queue.
48         */
49        public ThreadQueue() {
50                runnableQueue = new Queue();
51        }
52       
53        /**
54         * Flushes any pending work items, and optionally
55         * waits for the thread to join.
56         *
57         * @param wait True to wait for the thread to join.
58         */
59        public void shutdown(boolean wait) {
60                isShutdown = true;
61                synchronized(runnableQueue) {
62                        runnableQueue.clear();
63                }
64                if(wait && threadQueueThread != null) {
65                        try {
66                                threadQueueThread.join();
67                        } catch (InterruptedException e) { }
68                        threadQueueThread = null;
69                }
70        }
71       
72        /**
73         * Puts the provided <tt>Runnable</tt> object on the
74         * work item queue.  Starts the worker thread if necessary.
75         *
76         * @param runnable The <tt>Runnable</tt> object.
77         * @throws IllegalStateException Thrown if {@link #shutdown(boolean)} has been called.
78         */
79        public void invokeLater(Runnable runnable) {
80                if(isShutdown) {
81                        throw new IllegalStateException("Thread queue has been shutdown");
82                }
83                boolean queued = false;
84                synchronized(runnableQueue) {
85                        if(threadQueueThread != null && threadQueueThread.isAlive()) {
86                                runnableQueue.add(runnable);
87                                queued = true;
88                        }
89                }
90                if(!queued) {
91                        if(threadQueueThread != null) {
92                                try {
93                                        threadQueueThread.join();
94                                } catch (InterruptedException e) { }
95                                threadQueueThread = null;
96                        }
97                        threadQueueThread = new ThreadQueueThread();
98                        runnableQueue.add(runnable);
99                        threadQueueThread.start();
100                }
101        }
102       
103        /**
104         * Actual thread implementation used for the work item queue.
105         */
106        private class ThreadQueueThread extends Thread {
107                /**
108                 * Instantiates a new thread queue thread.
109                 */
110                public ThreadQueueThread() {
111                }
112
113                /* (non-Javadoc)
114                 * @see java.lang.Thread#run()
115                 */
116                public void run() {
117                        while(true) {
118                                Runnable runnable;
119                                synchronized(runnableQueue) {
120                                        if(runnableQueue.element() != null) {
121                                                runnable = (Runnable)runnableQueue.remove();
122                                        }
123                                        else {
124                                                return;
125                                        }
126                                }
127                                Thread.yield();
128                                try {
129                                        runnable.run();
130                                } catch (RuntimeException exp) {
131                                        System.err.println(exp.toString());
132                                }
133                        }
134                }
135        }
136}
Note: See TracBrowser for help on using the browser.