Java Program to Implement SynchronousQueue API Last Updated : 13 Aug, 2021 Comments Improve Suggest changes Like Article Like Report SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner. SynchronousQueue has only 2 supported operations: Both of these are blocking method which means when we want to add a piece of information or data in the queue, we call the put() method but this method will remain blocked or it will wait until some other thread calls take() method and allows the thread to take the data or information. 1. take() Java try { synchronousQueue.put("data or information goes here"); } catch(InterruptedException iex) { iex.printStackTrace(); } 2. put() Java try { // data type according to the data or information String info = synchronousQueue.take(); } catch(InterruptedException iex) { iex.printStackTrace(); } There are two types of constructors of SynchronousQueue which is based on two different access policy: 1. SynchronousQueue(): In this, if multiple threads are waiting then these threads are granted access randomly or unspecified manner, this is called no fair policy. 2. SynchronousQueue(boolean fair): In this, if multiple threads are waiting then these threads are granted access in FIFO(first in first out) manner. Implementation: Java // Java program to implement SynchronousQueue API. import java.util.concurrent.BlockingQueue; import java.util.concurrent.SynchronousQueue; public class SynchronousQAPI<E> { public SynchronousQueue<E> synchronousQ; // we create a SynchronousQueue with no fair policy public SynchronousQAPI() { synchronousQ = new SynchronousQueue<E>(); } // we create a SynchronousQueue with fair policy public SynchronousQAPI(boolean fair) { synchronousQ = new SynchronousQueue<E>(); } // As we discussed above in API overview that // SynchronousQueue has 2 supported operations put() and // take() So, we will implement this methods only // put() method: It insert element at tail of the queue // and used to wait until the queue is full. public void put(E e) throws InterruptedException { synchronousQ.put(e); } // take() method: return element at the head of the // queue public E take() throws InterruptedException { return synchronousQ.take(); } // Implementation of Put Thread (producer) class Put implements Runnable { @SuppressWarnings("rawtypes") BlockingQueue SynchronousQueue; @SuppressWarnings("rawtypes") public Put(BlockingQueue q) { this.SynchronousQueue = q; } @SuppressWarnings("unchecked") @Override public void run() { try { // put the data SynchronousQueue.put(1); System.out.println( "1 added to synchronous queue."); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } class Take implements Runnable { @SuppressWarnings("rawtypes") BlockingQueue SynchronousQueue; @SuppressWarnings("rawtypes") public Take(BlockingQueue q) { this.SynchronousQueue = q; } @Override public void run() { try { // take out the previously inserted data this.SynchronousQueue.take(); System.out.println( "1 removed from synchronous queue."); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws InterruptedException { SynchronousQAPI<Integer> synchronousQueue = new SynchronousQAPI<Integer>(); new Thread(new SynchronousQAPI<>().new Put( synchronousQueue.synchronousQ)) .start(); new Thread(new SynchronousQAPI<>().new Take( synchronousQueue.synchronousQ)) .start(); } } Output1 added to synchronous queue. 1 removed from synchronous queue. Comment More infoAdvertise with us Next Article Java Program to Implement SynchronousQueue API T TanmayChakraborty Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read Java Program to Implement PriorityBlockingQueue API PriorityBlockingQueue is an unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. The âblockingâ part of the name is added to imply the thread will block waiting until thereâs an item available on the queue. This class does not 2 min read Java Program to Implement DelayQueue API The DelayQueue class belongs to java.util.concurrent package. DelayQueue implements the BlockingQueue interface. DelayQueue is a specialized Priority Queue that orders elements supported their delay time. It means that only those elements can be taken from the queue whose time has expired. DelayQueu 7 min read Java Program to Implement LinkedTransferQueue API LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo 5 min read Java Program to Implement LinkedBlockingQueue API LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for 6 min read Java Program to Implement ArrayBlockingQueue API ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, 7 min read Java Program to Implement ConcurrentLinkedQueue API The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts e 4 min read Java Program to Implement ConcurrentLinkedDeque API ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also exte 3 min read How to Implement a Thread-Safe Resizable Array in Java? Multiple threads may securely execute operations like insertion and deletion without risking data corruption when utilizing a thread-safe resizable array. The ArrayList class is a popular Java class, yet it is not thread-safe by default. We may use concurrent collections or synchronization to make i 2 min read Java Threading Programs - Basic to Advanced Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral 3 min read Like