Browse Source

Make OutputQueue a bit thread safe.

Change-Id: Ib29494e43a7271f8511bc7f733534aae47aea8b2
Fixes-Issue: CLIENT-367
Reviewed-on: http://gerrit.dmdirc.com/2481
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.7rc1
Chris Smith 12 years ago
parent
commit
c504516f07
1 changed files with 21 additions and 10 deletions
  1. 21
    10
      src/com/dmdirc/parser/irc/outputqueue/OutputQueue.java

+ 21
- 10
src/com/dmdirc/parser/irc/outputqueue/OutputQueue.java View File

@@ -40,6 +40,8 @@ public class OutputQueue {
40 40
     private boolean queueEnabled = true;
41 41
     /** The output queue! */
42 42
     private final BlockingQueue<QueueItem> queue = new PriorityBlockingQueue<QueueItem>();
43
+    /** Object for synchronising access to the {@link #queueHandler}. */
44
+    private final Object queueHandlerLock = new Object();
43 45
     /** Thread for the sending queue. */
44 46
     private QueueHandler queueHandler;
45 47
     /** The QueueFactory for this OutputQueue. */
@@ -105,12 +107,15 @@ public class OutputQueue {
105 107
         if (out == null) {
106 108
             throw new NullPointerException("No output stream has been set.");
107 109
         }
110
+
108 111
         final boolean old = this.queueEnabled;
109 112
         this.queueEnabled = queueEnabled;
110 113
 
111 114
         if (old != queueEnabled && old) {
112
-            queueHandler.interrupt();
113
-            queueHandler = null;
115
+            synchronized (queueHandlerLock) {
116
+                queueHandler.interrupt();
117
+                queueHandler = null;
118
+            }
114 119
 
115 120
             while (!queue.isEmpty()) {
116 121
                 try {
@@ -127,9 +132,12 @@ public class OutputQueue {
127 132
      */
128 133
     public void clearQueue() {
129 134
         this.queueEnabled = false;
130
-        if (queueHandler != null) {
131
-            queueHandler.interrupt();
132
-            queueHandler = null;
135
+
136
+        synchronized (queueHandlerLock) {
137
+            if (queueHandler != null) {
138
+                queueHandler.interrupt();
139
+                queueHandler = null;
140
+            }
133 141
         }
134 142
 
135 143
         queue.clear();
@@ -167,13 +175,16 @@ public class OutputQueue {
167 175
         if (out == null) {
168 176
             throw new NullPointerException("No output stream has been set.");
169 177
         }
178
+
170 179
         if (queueEnabled) {
171
-            if (queueHandler == null || !queueHandler.isAlive()) {
172
-                queueHandler = queueFactory.getQueueHandler(this, queue, out);
173
-                queueHandler.start();
174
-            }
180
+            synchronized (queueHandlerLock) {
181
+                if (queueHandler == null || !queueHandler.isAlive()) {
182
+                    queueHandler = queueFactory.getQueueHandler(this, queue, out);
183
+                    queueHandler.start();
184
+                }
175 185
 
176
-            queue.add(queueHandler.getQueueItem(line, priority));
186
+                queue.add(queueHandler.getQueueItem(line, priority));
187
+            }
177 188
         } else {
178 189
             out.printf("%s\r\n", line);
179 190
         }

Loading…
Cancel
Save