1 20 package com.scalagent.kjoram.util; 21 22 import java.io.*; 23 import java.util.*; 24 25 37 public class Queue extends Vector 38 { 39 40 44 private boolean stopping; 45 46 47 50 public Queue() 51 { 52 super(); 53 start(); 54 } 55 56 57 63 public synchronized void push(Object item) 64 { 65 if (stopping) 66 throw new StoppedQueueException(); 67 68 addElement(item); 69 notify(); 70 } 71 72 73 79 public synchronized Object pop() 80 { 81 Object obj; 82 83 if (size() == 0) 84 throw new EmptyQueueException(); 85 86 obj =elementAt(0); 87 removeElementAt(0); 88 89 if (stopping && size() == 0) 90 notify(); 91 92 return obj; 93 } 94 95 96 102 public synchronized Object get() throws InterruptedException 103 { 104 while (size() == 0) 105 wait(); 106 107 return elementAt(0); 108 } 109 110 111 112 public void start() 113 { 114 stopping = false; 115 } 116 117 118 122 public synchronized void stop() throws InterruptedException 123 { 124 stopping = true; 125 126 if (size() != 0) 127 wait(); 128 } 129 } 130 | Popular Tags |