BlockingQueue: ============== -> BlockingQueue is an interface in java -> package: java.util.concurrent -> introduced from java 1.5. -> BlockingQueue is designed to handle the thread safe operations in concurrent environments. -> BlockingQueue commonly used in "producer-consumer" patterns and also in multi-thread operations. Advantages: =========== 1) Thread safe. 2) Blocking Operations blocking operations can be performed using blocking methods. blocking operations means: block the thread until the operation is completed. 3) never accept null elements. when we can add "null" elements, then we can get "NullPointerException". Hierarchy of BlockingQueue: =========================== Iterable Interface <================= Collection Interface extends Collection Interface <==================== Queue Interface extends Queue Interface <============================= BlockingQueue interface extends BlockingQueue Interface <=======================ArrayBlockingQueue class/LinkedBlockingQueue class implements --> BlockingQueue Interface consisting of two classes: 1) ArrayBlockingQueue 2) LinkedBlockingQueue Types of BlockingQueue: ====================== -> two types: 1) Unbounded BlockingQueue ========================== Syntax: BlockingQueue name_of_blocking_queue = new LinkedBlockingQueue<>(); package pack.p1; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class UnboundedBlckingQueue { public static void main(String[] args) { // Unbounded BlockingQueue BlockingQueue b = new LinkedBlockingQueue<>(); // adding elements try { b.put(-11); b.put(11); b.put(101); b.put(-101); System.out.println("The Given BlockingQueue = "+b); b.put(201); b.put(211); System.out.println("The BlockingQueue after the insertion elements = "+b); } catch(Exception e) { e.printStackTrace(); } } } ============================================================================= 2) Bounded BlockingQueue ======================== Syntax: BlockingQueue name_of_blocking_queue = new ArrayBlockingQueue<>(capacity); -> When we want to create an "unbounded BlockingQueue", we need to define that object with "ArrayBlockingQueue". -> When we add less number of elements than the original capacity: no error can be generated. package pack.p1; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class MainClass { public static void main(String[] args) { // Bounded Blocking Queue object BlockingQueue bq = new ArrayBlockingQueue<>(5); // adding elements into blocking queue try { bq.put(1001); bq.put(1003); bq.put(1234); bq.put(1023); // bq.put(1324); System.out.println("The Bounded BlockingQueue = "+bq); // bq.add(1302); // System.out.println("When the capacity is exceeded, the given Blocking Queue = "+bq); } catch(InterruptedException e) { e.printStackTrace(); } } } -============================================== -> If the number of elements into the BlockingQueue is same as the given capacity: then also no error. package pack.p1; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class MainClass { public static void main(String[] args) { // Bounded Blocking Queue object BlockingQueue bq = new ArrayBlockingQueue<>(5); // adding elements into blocking queue try { bq.put(1001); bq.put(1003); bq.put(1234); bq.put(1023); bq.put(1324); System.out.println("The Bounded BlockingQueue = "+bq); // bq.add(1302); // System.out.println("When the capacity is exceeded, the given Blocking Queue = "+bq); } catch(InterruptedException e) { e.printStackTrace(); } } } ================================================== -> when the number of elements into BlockingQueue can be exceeded the given capacity: here we can get an exception "IllegalStateException". -> IllegalStateException can be observed when the queue is full. package pack.p1; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class MainClass { public static void main(String[] args) { // Bounded Blocking Queue object BlockingQueue bq = new ArrayBlockingQueue<>(5); // adding elements into blocking queue try { bq.put(1001); bq.put(1003); bq.put(1234); bq.put(1023); bq.put(1324); System.out.println("The Bounded BlockingQueue = "+bq); bq.add(1302); System.out.println("When the capacity is exceeded, the given Blocking Queue = "+bq); } catch(InterruptedException e) { e.printStackTrace(); } } } Output: ====== The Bounded BlockingQueue = [1001, 1003, 1234, 1023, 1324] Exception in thread "main" java.lang.IllegalStateException: Queue full at java.base/java.util.AbstractQueue.add(AbstractQueue.java:98) at java.base/java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:329) at BlockingQueueDemo/pack.p1.MainClass.main(MainClass.java:21) ===================================================================== Practice Program: ================= package pack.p1; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class RestaurentOrder { public static void main(String[] args) { BlockingQueue order = new LinkedBlockingQueue<>(); try { System.out.println("Cashier: Placing Order for Burger:"); order.put("Burger"); System.out.println("Cashier: Placing Order for Pizza:"); order.put("Pizza"); System.out.println("Cashier Placing Order for Fries:"); order.put("Fries"); System.out.println("Cashier: Placing Order for Nuggets:"); order.put("Nuggets"); System.out.println("Cashier Placing Order for Biryani:"); order.put("Biryani"); System.out.println("Cashier: Placing Order for Pizza:"); order.put("Pizza"); System.out.println("Cashier: Placing Order for Burger:"); order.put("Burger"); System.out.println("The Orders which were placed:"+order); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); System.out.println("\nKitchen Preparing:"+order.take()); } catch(Exception e) { e.printStackTrace(); } } }