1 2 5 14 package org.jacorb.trading.util; 15 16 19 public class MessageQueue 20 { 21 private boolean m_deactivated = false; 22 private Element m_head = null; 23 private Element m_tail = null; 24 25 26 private static class Element 27 { 28 public Object value; 29 public Element next; 30 31 public Element(Object val) 32 { 33 value = val; 34 next = null; 35 } 36 } 37 38 39 public MessageQueue() 40 { 41 } 42 43 44 public synchronized void deactivate() 45 { 46 m_deactivated = true; 47 notifyAll(); 48 } 49 50 51 public synchronized boolean getDeactivated() 52 { 53 return m_deactivated; 54 } 55 56 57 public synchronized boolean getEmpty() 58 { 59 return (m_head == null); 60 } 61 62 63 public synchronized void enqueue(Object value) 64 { 65 Element elem = new Element(value); 66 if (m_tail == null) 67 m_head = m_tail = elem; 68 else { 69 m_tail.next = elem; 70 m_tail = elem; 71 } 72 73 notifyAll(); 75 } 76 77 78 public synchronized Object dequeue() 79 { 80 Object result = null; 81 82 while (result == null && ! m_deactivated) { 83 if (m_head == null) { 85 try { 86 wait(); 87 } 88 catch (InterruptedException e) { 89 } 90 } 91 else { 92 result = m_head.value; 94 m_head = m_head.next; 95 if (m_head == null) 96 m_tail = null; 97 } 98 } 99 100 return result; 101 } 102 103 104 166 } 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | Popular Tags |