1 46 package org.mr.core.util; 47 48 import java.util.LinkedList ; 49 import java.util.List ; 50 51 import org.apache.commons.logging.LogFactory; 52 53 59 public class SynchronizedQueue implements Queue { 60 protected List m_list; 61 62 63 67 public SynchronizedQueue(){ 68 m_list=new LinkedList (); 69 70 } 71 72 77 synchronized public boolean enqueue(Object o){ 78 boolean b = m_list.add(o) ; 79 80 notifyAll(); return b; 82 } 83 84 89 synchronized public Object dequeue() { 90 while(m_list.size() ==0) { 91 try { 92 wait(); 93 } catch (InterruptedException ex){ 94 if(LogFactory.getLog("SynchronizedQueue").isFatalEnabled()) 95 LogFactory.getLog("SynchronizedQueue").fatal("core Queue got at Exception" , ex); 96 } 97 } 98 return m_list.remove(0); 99 } 100 101 106 synchronized public Object dequeueNoBlock(){ 107 if(m_list.size() ==0)return null; 108 return m_list.remove(0); 109 } 110 111 118 synchronized public Object dequeue(long timeout){ 119 if(m_list.size() ==0) { 120 try { 121 wait(timeout); 122 } catch (InterruptedException ex){ 123 if(LogFactory.getLog("SynchronizedQueue").isFatalEnabled()) 124 LogFactory.getLog("SynchronizedQueue").fatal("core Queue got at Exception" , ex); 125 } 126 } 127 return dequeueNoBlock(); 128 129 } 130 135 synchronized public int size() { 136 return m_list.size(); 137 } 138 139 140 145 synchronized public boolean isEmpty(){ 146 return (size()==0); 147 } 148 149 150 151 152 156 public synchronized void clear(){ 157 while(!isEmpty()){ 158 this.dequeue(); 159 } 160 } 161 162 public List getUnderlineList(){ 163 return m_list; 164 } 165 166 } 167 | Popular Tags |