Java Program to Implement SynchronosQueue API

This Java program Implements SynchronousQueue API.A blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa. A synchronous queue does not have any internal capacity, not even a capacity of one. You cannot peek at a synchronous queue because an element is only present when you try to remove it; you cannot insert an element (using any method) unless another thread is trying to remove it; you cannot iterate as there is nothing to iterate. The head of the queue is the element that the first queued inserting thread is trying to add to the queue; if there is no such queued thread then no element is available for removal and poll() will return null.

Here is the source code of the Java Program to Implement SynchronosQueue. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
 
public class SynchronousQueueImpl<E>
{
    private SynchronousQueue<E> synchronousQueue;
 
    /** Creates a SynchronousQueue with nonfair access policy. **/
    public SynchronousQueueImpl()
    {
        synchronousQueue = new SynchronousQueue<E>();
    }
 
    /** Creates a SynchronousQueue with the specified fairness policy. **/
    public SynchronousQueueImpl(boolean fair)
    {
        synchronousQueue = new SynchronousQueue<E>(fair);
    }
 
    /** Inserts the specified element at the tail of this queue. **/
    public boolean add(E e)
    {
        return synchronousQueue.add(e);
    }
 
    /** Atomically removes all of the elements from this queue. **/
    public void clear()
    {
        synchronousQueue.clear();
    }
 
    /** Returns true if this queue contains the specified element. **/
    public boolean contains(Object o)
    {
        return synchronousQueue.contains(o);
    }
 
    /**
     * Removes all available elements from this queue and adds them to the given
     * collection.
     **/
    public int drainTo(Collection<? super E> c)
    {
        return synchronousQueue.drainTo(c);
    }
 
    /**
     * Removes at most the given number of available elements from this queue
     * and adds them to the given collection.
     **/
    public int drainTo(Collection<? super E> c, int maxElements)
    {
        return synchronousQueue.drainTo(c, maxElements);
    }
 
    /** Returns an iterator over the elements in this queue in proper sequence. **/
    public Iterator<E> iterator()
    {
        return synchronousQueue.iterator();
    }
 
    /**
     * Inserts the specified element at the tail of this queue if it is possible
     * to do so immediately without exceeding the queue's capacity, returning
     * true upon success and false if this queue is full.
     **/
    public boolean offer(E e)
    {
        return synchronousQueue.offer(e);
    }
 
    /**
     * Inserts the specified element at the tail of this queue, waiting up to
     * the specified wait time for space to become available if the queue is
     * full.
     **/
    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
    {
        return synchronousQueue.offer(e, timeout, unit);
    }
 
    /**
     * Retrieves, but does not remove, the head of this queue, or returns null
     * if this queue is empty.
     **/
    public E peek()
    {
        return synchronousQueue.peek();
    }
 
    /**
     * Retrieves and removes the head of this queue, or returns null if this
     * queue is empty.
     **/
    public E poll()
    {
        return synchronousQueue.poll();
    }
 
    /**
     * Retrieves and removes the head of this queue, waiting up to the specified
     * wait time if necessary for an element to become available.
     **/
    public E poll(long timeout, TimeUnit unit) throws InterruptedException
    {
        return synchronousQueue.poll(timeout, unit);
    }
 
    /**
     * Inserts the specified element at the tail of this queue, waiting for
     * space to become available if the queue is full.
     **/
    public void put(E e) throws InterruptedException
    {
        synchronousQueue.put(e);
    }
 
    /**
     * Always returns Integer.MAX_VALUE because a PriorityBlockingQueue is not
     * capacity constrained.
     **/
    public int remainingCapacity()
    {
        return synchronousQueue.remainingCapacity();
    }
 
    /**
     * Removes a single instance of the specified element from this queue, if it
     * is present.
     **/
    public boolean remove(Object o)
    {
        return synchronousQueue.remove(o);
    }
 
    /** Returns the number of elements in this queue. **/
    public int size()
    {
        return synchronousQueue.size();
    }
 
    /**
     * Retrieves and removes the head of this queue, waiting if necessary until
     * an element becomes available
     **/
    public E take() throws InterruptedException
    {
        return synchronousQueue.take();
    }
 
    /**
     * Returns an array containing all of the elements in this queue, in proper
     * sequence.
     **/
    public Object[] toArray()
    {
        return synchronousQueue.toArray();
    }
 
    /**
     * Returns an array containing all of the elements in this queue, in proper
     * sequence; the runtime type of the returned array is that of the specified
     * array.
     **/
    public <T> T[] toArray(T[] a)
    {
        return synchronousQueue.toArray(a);
    }
 
    /** Returns a string representation of this collection. **/
    public String toString()
    {
        return synchronousQueue.toString();
    }
 
    public static void main(String... arg) throws InterruptedException
    {
        SynchronousQueueImpl<Integer> synchronousQueue = new SynchronousQueueImpl<Integer>();
        new Thread(new SynchronousQueueImpl<>().new PutThread(
        synchronousQueue.synchronousQueue)).start();
        new Thread(new SynchronousQueueImpl<>().new TakeThread(
        synchronousQueue.synchronousQueue)).start();
    }
 
    class PutThread implements Runnable
    {
        BlockingQueue SynchronousQueue;
 
        public PutThread(BlockingQueue q)
        {
            this.SynchronousQueue = q;
        }
 
        @Override
        public void run()
        {
            try
            {
                SynchronousQueue.put(19);
                System.out.println("19 added to synchronous queue by PutThread");
                Thread.sleep(1000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
 
    class TakeThread implements Runnable
    {
        BlockingQueue SynchronousQueue;
 
        public TakeThread(BlockingQueue q)
        {
            this.SynchronousQueue = q;
        }
 
        @Override
        public void run()
        {
            try
            {
                this.SynchronousQueue.take();
                System.out.println("19 removed from synchronous queue by TakeThread");
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}
$ javac SynchronousQueueImpl.java
$ java SynchronousQueueImpl
19 added to synchronous queue by PutThread
19 removed from synchronous queue by TakeThread

Related posts:

Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Spring 5 Testing with @EnabledIf Annotation
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement Queue
Send an email with an attachment
Annotation trong Java 8
Java Program to Implement RoleUnresolvedList API
Java Program to Implement ScapeGoat Tree
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement a Binary Search Tree using Linked Lists
Introduction to the Java NIO2 File API
Java Program to Perform Sorting Using B-Tree
Convert Hex to ASCII in Java
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Find MST (Minimum Spanning Tree) using Kruskal’s Algorithm
Setting Up Swagger 2 with a Spring REST API
Java Program to Implement the Vigenere Cypher
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
HttpAsyncClient Tutorial
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Database Migrations with Flyway
Java – Generate Random String
Spring Boot - Scheduling
Java Program to Implement Knight’s Tour Problem
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Hướng dẫn sử dụng Java Reflection
Java Program to Implement SimpeBindings API
Java Program to Implement Repeated Squaring Algorithm