1 21 package fr.dyade.aaa.util; 22 23 import java.io.*; 24 import java.util.*; 25 26 38 public class Queue extends Vector { 39 43 private boolean stopping; 44 45 48 public Queue() { 49 super(); 50 start(); 51 } 52 53 54 60 public synchronized void push(Object item) { 61 if (stopping) 62 throw new StoppedQueueException(); 63 64 addElement(item); 65 notify(); 66 } 67 68 69 75 public synchronized Object pop() { 76 Object obj; 77 78 if (size() == 0) 79 throw new EmptyQueueException(); 80 81 obj =elementAt(0); 82 removeElementAt(0); 83 84 if (stopping && size() == 0) 85 notify(); 86 87 return obj; 88 } 89 90 91 97 public synchronized Object get() throws InterruptedException { 98 while (size() == 0) 99 wait(); 100 101 return elementAt(0); 102 } 103 104 105 106 public void start() { 107 stopping = false; 108 } 109 110 111 115 public synchronized void stop() throws InterruptedException { 116 stopping = true; 117 if (size() != 0) wait(); 118 } 119 } 120 | Popular Tags |