1 48 49 50 package org.exolab.jms.util; 51 52 import java.util.LinkedList ; 53 54 55 66 public class BlockingWorkerQueue 67 extends Thread { 68 69 72 private QueueWorker _worker = null; 73 74 77 private LinkedList _queue = new LinkedList (); 78 79 82 private boolean _closed = false; 83 84 85 92 public BlockingWorkerQueue(String name, QueueWorker worker) { 93 super("BWQ:" + name); 94 _worker = worker; 95 this.setDaemon(true); 96 } 97 98 103 public void add(Object object) { 104 synchronized (_queue) { 105 _queue.addLast(object); 106 _queue.notifyAll(); 107 } 108 } 109 110 116 public void run() { 117 while (!_closed) { 118 Object work = null; 119 120 synchronized (_queue) { 121 if (_queue.size() == 0) { 123 try { 124 _queue.wait(); 125 } catch (InterruptedException exception) { 126 } 128 continue; 129 } 130 131 work = _queue.removeFirst(); 134 } 135 136 if (work != null) { 137 _worker.execute(work); 138 } 139 } 140 } 141 142 147 public int size() { 148 return _queue.size(); 149 } 150 151 154 public void close() { 155 _closed = true; 156 interrupt(); 157 } 158 159 } 160 161 | Popular Tags |