1 23 24 29 42 43 48 49 package com.sun.enterprise.util.collection; 50 51 import java.util.LinkedList ; 52 import java.util.Collection ; 53 54 import com.sun.enterprise.util.pool.TimedoutException; 55 import java.util.logging.Logger ; 57 import java.util.logging.Level ; 58 import com.sun.logging.LogDomains; 59 61 65 66 71 public class BlockingQueue { 72 73 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 75 private boolean closed = false; 77 private boolean aborted = false; 78 private int limit; 79 private LinkedList list; 80 private int waiters=0; 82 88 public BlockingQueue() { 89 this(Integer.MAX_VALUE); 90 } 91 92 99 public BlockingQueue(int queueLimit) { 100 this.limit = queueLimit; 101 this.list = new LinkedList (); 102 com.sun.enterprise.util.MonitorTask.addORBMonitorable(this); 104 } 106 107 108 111 public void addFirst(Object object) 112 throws TooManyTasksException, QueueClosedException 113 { 114 if (closed) 115 throw new QueueClosedException("Queue closed."); 116 synchronized (list) { 117 if (list.size() >= limit) { 118 throw new TooManyTasksException("Too many tasks in queue..."); 119 } 120 list.addFirst(object); 121 list.notify(); 122 } 123 } 124 125 128 public void addLast(Object object) 129 throws TooManyTasksException, QueueClosedException 130 { 131 if (closed) 132 throw new QueueClosedException("Queue closed."); 133 synchronized (list) { 134 if (list.size() >= limit) { 135 throw new TooManyTasksException("Too many tasks in queue..."); 136 } 137 list.add(object); 138 list.notify(); 139 } 140 } 141 142 145 public void add(int index, Object object) 146 throws TooManyTasksException, QueueClosedException 147 { 148 if (closed) 149 throw new QueueClosedException("Queue closed."); 150 synchronized (list) { 151 if (list.size() >= limit) { 152 throw new TooManyTasksException("Too many tasks in queue..."); 153 } 154 list.add(index, object); 155 list.notify(); 156 } 157 } 158 159 164 public void addAll(Collection c) 165 throws TooManyTasksException, QueueClosedException 166 { 167 if (closed) 168 throw new QueueClosedException("Queue closed."); 169 synchronized (list) { 170 if (list.size() >= limit) { 171 throw new TooManyTasksException("Too many tasks in queue..."); 172 } 173 list.addAll(c); 174 list.notify(); 175 } 176 } 177 178 179 182 public int size() { 183 synchronized (list) { 184 return list.size(); 185 } 186 } 187 188 190 193 public int getUnsyncSize () { 194 return list.size(); 195 } 196 197 200 public int getUnsyncWaitingThreads () { 201 return waiters; 202 } 203 204 207 public String toString() { 208 StringBuffer sb = new StringBuffer (); 209 sb.append("BlockingQueue [TW=").append(waiters); 210 sb.append(", CS=").append(list.size()); 211 sb.append(", MS=").append(limit).append("]"); 212 return sb.toString(); 213 } 214 216 221 public Object remove(boolean canWait) 222 throws InterruptedException , QueueClosedException 223 { 224 while (true) { 225 if (aborted) { 226 throw new QueueClosedException("Queue closed...."); 227 } 228 synchronized (list) { 229 if (list.size() > 0) { 230 return list.removeFirst(); 235 } 236 237 if (closed) { 238 throw new QueueClosedException("Queue closed...."); 239 } else { 240 if (! canWait) { 241 return null; 242 } 243 waiters++; list.wait(); 249 waiters--; } 251 } 252 } 253 } 254 255 256 261 public Object remove(long waitFor) 262 throws InterruptedException , QueueClosedException 263 { 264 if (aborted) { 266 throw new QueueClosedException("Queue closed...."); 267 } 268 synchronized (list) { 269 if (list.size() > 0) { 270 return list.removeFirst(); 275 } 276 277 if (closed) { 278 throw new QueueClosedException("Queue closed...."); 279 } else { 280 waiters++; list.wait(waitFor); 282 waiters--; if (list.size() > 0) { 284 return list.removeFirst(); 289 } else { 290 return null; 292 } 293 } 294 } } 296 297 public void shutdown() { 298 this.closed = true; 299 synchronized (list) { 300 list.notifyAll(); 301 } 302 } 303 304 public void abort() { 305 this.closed = this.aborted = true; 306 synchronized (list) { 307 list.notifyAll(); 308 } 309 } 310 311 312 } 313 | Popular Tags |