Skip to main content

Concurrency in Java: Producer-consumer problem using BlockingQueue

The java.util.concurrent.BlockingQueue is a queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element and wait for space to become available in the queue when storing an element.

A BlockingQueue may be capacity-bound and does not accept null elements.

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control.

BlockingQueue methods

BlockingQueue methods come in four forms for handling operations that cannot be satisfied immediately but may be satisfied at some point in the future:

1) Throws exception
2) Special value (null or false)
3) Blocks
4) Times out

                                                                                               
Throws exceptionSpecial valueBlocksTimes out
Insertadd(e)offer(e)put(e)offer(e, time, unit)
Removeremove()poll()take()poll(time, unit)
Examineelement()peek()not applicablenot applicable

Producer-Consumer using BlockingQueue

A BlockingQueue can be safely used with multiple producers and multiple consumers.

...
...
Produced: Thread-1, RemainingCapacity: 3
Produced: Thread-1, RemainingCapacity: 8
Consumed: Thread-3, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 10
Produced: Thread-0, RemainingCapacity: 8
Produced: Thread-0, RemainingCapacity: 9
Produced: Thread-0, RemainingCapacity: 8
Consumed: Thread-2, RemainingCapacity: 9
Consumed: Thread-2, RemainingCapacity: 9
Consumed: Thread-2, RemainingCapacity: 10
Consumed: Thread-3, RemainingCapacity: 9
Produced: Thread-1, RemainingCapacity: 7
Produced: Thread-0, RemainingCapacity: 8
Produced: Thread-1, RemainingCapacity: 9
Produced: Thread-1, RemainingCapacity: 8
Produced: Thread-1, RemainingCapacity: 7
Produced: Thread-1, RemainingCapacity: 6
...
...

To know more about Producer-Consumer Problem visit: Producer-Consumer Problem.



Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...