Skip to content

Commit 09a1fdf

Browse files
author
Simon MacMullen
committed
Don't synchronise the queue.put() since it may block. The queue itself is thread-safe by definition, and nothing should change the pool members under us, so this should be safe.
1 parent 8fa1f70 commit 09a1fdf

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/com/rabbitmq/client/impl/WorkPool.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,26 @@ private static <W> int drainTo(BlockingQueue<W> deList, Collection<W> c, int max
165165
* &mdash; <i>as a result of this work item</i>
166166
*/
167167
public boolean addWorkItem(K key, W item) {
168+
BlockingQueue<W> queue;
168169
synchronized (this) {
169-
BlockingQueue<W> queue = this.pool.get(key);
170-
if (queue != null) {
171-
try {
172-
queue.put(item);
173-
} catch (InterruptedException e) {
174-
// ok
175-
}
170+
queue = this.pool.get(key);
171+
}
172+
// The put operation may block. We need to make sure we are not holding the lock while that happens.
173+
if (queue != null) {
174+
try {
175+
queue.put(item);
176+
} catch (InterruptedException e) {
177+
// ok
178+
}
176179

180+
synchronized (this) {
177181
if (isDormant(key)) {
178182
dormantToReady(key);
179183
return true;
180184
}
181185
}
182-
return false;
183186
}
187+
return false;
184188
}
185189

186190
/**

0 commit comments

Comments
 (0)