Custom BlockingQueue implementation in java

Custom BlockingQueue in java

In this post, we will see how to create your own custom BlockingQueue.

This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts.

Here is simple implementation of BlockingQueue.

  • We will use array to store elements in BlockingQueue internally. Size of this array defines maximum number of  elements that can reside in BlockingQueue at a time.
  • We will use lock and conditions objects to create custom BlockingQueue.
  • While putting the element in the queue, if the queue is full, then the producer will wait for queue to empty.
  • While consuming element from the queue, if the queue is empty then the consumer will wait for the queue to get filled.

Create a class named CustomBlockingQueue.java

Create another main class which will use above CustomBLockingQueue.

We have created two Runnable classes, one for producer and another for consumer and created two threads using these runnables.

Producing – 1
Producing – 2
Producing – 3
Producing – 4
Producing – 5
Producing – 6
Consuming – 1
Consuming – 2
Consuming – 3
Consuming – 4
Consuming – 5
Consuming – 6
Producing – 7
Producing – 8
Producing – 9
Producing – 10
Consuming – 7
Consuming – 8
Consuming – 9
Consuming – 10

Output may vary for you but there can be only 6 elements at a time in the CustomBlockingQueue.
That’s all about Custom BlockingQueue implementation in java


import_contacts

You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • Delay java program by few seconds
    11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. Using Thread.sleep Sleep method causes current thread to pause for […]

  • 29 May

    How to print even and odd numbers using threads in java

    In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.