Advertisement
Guest User

Blocking Queue

a guest
Jun 19th, 2011
1,929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. public class BlockingQueue {
  2.  
  3.   private List queue = new LinkedList();
  4.   private int  maxSize = 10;
  5.  
  6.   public BlockingQueue(int limit){
  7.     this.maxSize = maxSize;
  8.   }
  9.  
  10.  
  11.   public synchronized void enqueue(Object item) throws InterruptedException  {
  12.     while(this.queue.size() == this.maxSize) {
  13.       wait();
  14.     }
  15.     if(this.queue.size() == 0) {
  16.       notifyAll();
  17.     }
  18.     this.queue.add(item);
  19.   }
  20.  
  21.  
  22.   public synchronized Object dequeue() throws InterruptedException{
  23.     while(this.queue.size() == 0){
  24.       wait();
  25.     }
  26.     if(this.queue.size() == this.maxSize){
  27.       notifyAll();
  28.     }
  29.  
  30.     return this.queue.remove(0);
  31.   }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement