source: trunk/LogicMail/src/org/logicprobe/LogicMail/util/ThreadQueue.java @ 935

Revision 935, 4.8 KB checked in by octorian, 6 months ago (diff)

Added analytics code to all error-level logged exceptions

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