1 package org.exoplatform.services.threadpool.impl; 2 27 28 import java.util.ArrayList ; 29 30 public class Queue { 31 private ArrayList data = new ArrayList (); 32 private int maxQueueSize = Integer.MAX_VALUE; 33 34 42 synchronized public void put(Object obj) throws InterruptedException { 43 if (Thread.currentThread().isInterrupted()) throw new InterruptedException (); 44 if (obj == null) throw new IllegalArgumentException ("null"); 45 while (data.size() >= maxQueueSize) { 46 try { 47 wait(); 48 } catch (InterruptedException e) { 49 return; 50 } } data.add(obj); 53 notify(); 54 } 56 71 synchronized public boolean put(Object obj, long msecs) throws InterruptedException { 72 if (Thread.currentThread().isInterrupted()) throw new InterruptedException (); 73 if (obj == null) throw new IllegalArgumentException ("null"); 74 long startTime = System.currentTimeMillis(); 75 long waitTime = msecs; 76 while (data.size() >= maxQueueSize) { 77 waitTime = msecs - (System.currentTimeMillis() - startTime); 78 if (waitTime <= 0) return false; 79 wait(waitTime); 80 } data.add(obj); 82 notify(); 83 return true; 84 } 86 90 synchronized public Object get() throws InterruptedException { 91 while (data.size() == 0) { 92 wait(); 93 } Object obj = data.remove(0); 95 notify(); 96 return obj; 97 } 99 111 synchronized public Object get(long msecs) throws InterruptedException { 112 long startTime = System.currentTimeMillis(); 113 long waitTime = msecs; 114 115 if (data.size() > 0) return data.remove(0); 116 while (true) { 117 waitTime = msecs - (System.currentTimeMillis() - startTime); 118 if (waitTime <= 0) return null; 119 wait(waitTime); 120 if (data.size() > 0) { 121 Object obj = data.remove(0); 122 notify(); 123 return obj; 124 } } } 128 131 public void setMaxQueueSize(int newValue) { 132 maxQueueSize = newValue; 133 } 134 } | Popular Tags |