Advertisement
Jaydeep999997

Blocking Queue - S1

Oct 21st, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | Source Code | 0 0
  1. class Demonstration {
  2.   public static void main(String args[]) throws Exception {
  3.     test();
  4.   }
  5.  
  6.   private synchronized static void safePrint(String message) {
  7.       System.out.println(message);
  8.     }
  9.  
  10.   private static void test() throws Exception {
  11.     final BlockingQueue<Integer> queue = new BlockingQueue<Integer>(5);
  12.    
  13.    
  14.    
  15.     Thread t1 = new Thread(new Runnable() {
  16.      
  17.       @Override
  18.       public void run() {
  19.         try {
  20.           for(int i = 0; i < 5; i++) {
  21.             queue.enqueue(new Integer(i));
  22.             safePrint("Enqueued: " + i);
  23.           }
  24.         } catch (InterruptedException ie) {
  25.        
  26.         }
  27.       }
  28.     }
  29.     );
  30.    
  31.     Thread t2 = new Thread(new Runnable() {
  32.      
  33.       @Override
  34.       public void run() {
  35.         try {
  36.           for(int i = 0; i < 3; i++) {
  37.             safePrint("Dequeuedby T2: " + queue.deque());
  38.           }
  39.         } catch (InterruptedException ie) {
  40.        
  41.         }
  42.       }
  43.     }
  44.     );
  45.  
  46.       Thread t3 = new Thread(new Runnable() {
  47.      
  48.       @Override
  49.       public void run() {
  50.         try {
  51.           for(int i = 0; i < 2; i++) {
  52.             safePrint("Dequeuedby T3: " + queue.deque());
  53.           }
  54.         } catch (InterruptedException ie) {
  55.        
  56.         }
  57.       }
  58.     }
  59.     );
  60.  
  61.   t1.start();
  62.   t2.start();
  63.   t3.start();
  64.  
  65.   t1.join();
  66.   t2.join();
  67.   t3.join();
  68. }
  69. }
  70.  
  71. // Blocking queue class
  72. class BlockingQueue<T> {
  73.   private int currentSize = 0;
  74.   private int head = 0, tail = 0;
  75.   private int maxCapacity;
  76.   T[] queue;
  77.   Object lock = new Object();
  78.  
  79.   @SuppressWarnings("unchecked")
  80.   public BlockingQueue(int maxCapacity) {
  81.     this.maxCapacity = maxCapacity;
  82.     this.queue = (T[]) new Object[maxCapacity];
  83.   }
  84.  
  85.   public void enqueue(T value) throws InterruptedException {
  86.     synchronized (lock) {
  87.       while(currentSize == maxCapacity) {
  88.         lock.wait();
  89.       }
  90.      
  91.       if(tail == maxCapacity) {
  92.         tail = 0;
  93.       }
  94.      
  95.       queue[tail] = value;
  96.       tail++;
  97.       currentSize++;
  98.      
  99.       lock.notifyAll();
  100.     }
  101.   }
  102.    
  103.  
  104.   public T deque() throws InterruptedException {
  105.     T value = null;
  106.     synchronized (lock) {
  107.       while(currentSize == 0) {
  108.         lock.wait();
  109.       }
  110.      
  111.       if(head == maxCapacity) {
  112.         head = 0;
  113.       }
  114.      
  115.       value = queue[head];
  116.       queue[head] = null;
  117.       head++;
  118.       currentSize--;
  119.      
  120.       lock.notifyAll();
  121.     }
  122.    
  123.     return value;
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement