Skip to main content

Concurrency in Java: Producer-Consumer Problem

Problem

The producer-consumer problem (bounded buffer problem) describes two processes, the producer and the consumer.

Both Producer and Consumer share a common memory buffer of a certain size.

The producer produces the data in the buffer, and the consumer consumes the data from the buffer.

The task is to make sure that the producer won't try to add data to the buffer if it's full and the consumer won't try to remove data from an empty buffer.

Solution

If the buffer is empty, the consumer is to go to sleep. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.

If the buffer is full, the producer is to either go to sleep or discard data. The next time the consumer consumes (removes) an item from the buffer, it notifies the producer, who starts to fill the buffer again.

Implementation

Producer-consumer problems can be solved using Java concurrency constructs such as wait() and notify().

The idea is to create a "Task" class containing both the "data buffer (LinkedList)" and the "consume() and produce()" methods.

Make sure to mark the consume() and produce() methods with the "synchronized" keyword. This will make sure that only one thread is able to execute either of the two consume() or produce() methods on the same object.

Now, the process is simple: if the buffer is empty, make the "consumer" thread wait, and if the buffer is full, make the "producer" thread wait.

...

Produced: true, Queue size: 6
Produced: true, Queue size: 7
Produced: true, Queue size: 8
Produced: true, Queue size: 9
Produced: true, Queue size: 10
Consumed: Something !!!, Queue size: 9
Consumed: Something !!!, Queue size: 8
Consumed: Something !!!, Queue size: 7
Consumed: Something !!!, Queue size: 6
Consumed: Something !!!, Queue size: 5
Consumed: Something !!!, Queue size: 4
Produced: true, Queue size: 5
Produced: true, Queue size: 6
Produced: true, Queue size: 7
Produced: true, Queue size: 8
Produced: true, Queue size: 9
Produced: true, Queue size: 10
Consumed: Something !!!, Queue size: 9
Consumed: Something !!!, Queue size: 8
Consumed: Something !!!, Queue size: 7
Consumed: Something !!!, Queue size: 6
Consumed: Something !!!, Queue size: 5
Consumed: Something !!!, Queue size: 4
Consumed: Something !!!, Queue size: 3
Consumed: Something !!!, Queue size: 2
Consumed: Something !!!, Queue size: 1
Consumed: Something !!!, Queue size: 0
Produced: true, Queue size: 1
....
....

Related: Producer-consumer problem using BlockingQueue



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...

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

Feign , an early declarative REST client, was initially part of the Spring Cloud Netflix stack and later rebranded as Spring Cloud OpenFeign . Before its introduction, crafting HTTP calls using RestTemplate involved repetitive code for each service interaction. With Feign, developers could simply define an interface with method contracts mirroring the service's endpoints. Behind the scenes, proxy magic generated a fully functional HTTP client, eliminating the need for boilerplate code . HTTP Interface (Spring Framework 6) The recent release of Spring Framework 6 integrated this declarative REST client as a native part of the core web framework in the form of the HTTP Interface . All the necessary components reside in the spring-web module, which is a transitive dependency for either the spring-boot-starter-web or spring-boot-starter-webflux modules. Currently, the WebFlux dependency is essential due to the HttpServiceProxyFactory , responsible for client gener...