Skip to main content

Concurrency in Java: using wait(), notify() and notifyAll()

Signaling is used to synchronise multiple threads by signalling to each other that a new thread can do something that was previously blocked by calling the wait() method.

In Java, thread signalling is implemented via the wait(), notify(), and notifyAll() methods that are part of the "Object" class.

A thread that calls "wait()" on any object becomes inactive (blocked) until another thread calls "notify()" or "notifyAll()" on that object.

In order to call "wait(), notify(), or notifyAll()" on an object, a thread must obtain the lock on that object first. That is, the calling thread must call wait(), notify(), or notifyAll() from inside a synchronized block or method.

If a thread tries to call "wait(), notify(), or notifyAll()" on an object without holding the synchronization lock on that object, an IllegalMonitorStateException is thrown.

In the example above, when a thread enters the synchronized block and calls wait() on the monitor object, the calling thread releases the lock on the monitor object and is "blocked" until another thread calls notify() or notifyAll() on the monitor object.

When another thread enters the synchronized block and calls the "notify()" method on the monitor object, it wakes up any one of the threads blocked by the wait() call on the same monitor object.

However, this awakened thread cannot exit the wait() method until the thread calling notify() has released the lock on the monitor object.

Multiple threads can call "wait()" on the same monitor object and thus become blocked waiting for a "notify() or notifyAll()" call.

Difference between notify() and notifyAll()

The main difference between the "notify()" and "notifyAll()" methods is that, if multiple threads are waiting on any locks in Java, the notify method sends a notification to only one of the waiting threads, while notifyAll informs all threads waiting on that lock.

If you use "notifyAll()", since all threads will be notified, they will compete for a lock, and the lucky thread that gets a lock can continue.

In a way, the notifyAll() method is safer because it sends a notification to all threads, so if any thread misses the notification, there are other threads to do the job.

Calling both of these methods does not give up the lock on the resource; rather, their job is to wake up the threads that have been sent to the waiting state using the "wait()" method.


It is mandatory to enclose wait() in a try-catch block because if a thread present in the waiting state gets interrupted, then it will throw an InterruptedException.

The wait() method variations

The wait() method has three variations:

1) wait(): This will cause the thread to wait until "notify()" or "notifyAll()" is called.

2) wait(long timeout): This will cause the thread to wait either until "notify()" or "notifyAll()" is called or until the timeout duration expires.

3) wait(long timeout, int nanoseconds): This will cause the thread to wait either until "notify()" or "notifyAll()" is called or until the timeout duration expires.

Missed Signals

A missed signal can occur when you have two threads where one calls "notify()" before the other calls "wait()".

In some cases, this may result in the waiting thread waiting forever and never waking up because the signal to wake up was missed.

In order to avoid losing signals, they should be stored in a member variable inside the signal class.

In the above-mentioned modified version of signalling implementation, the thread calling the "doNotify()" method sets the isSignalled variable to true before calling notify().

Likewise, the thread calling the "doWait()" method now checks the "isSignalled" variable before calling wait() and only calls wait() if no signal was received in between.

Exercise

Print alternate even and odd numbers using two threads

Given an integer "n," the task is to print numbers from "1" to "n" in increasing order using two threads.

Also, make sure that even numbers are printed by one thread and odd numbers are printed by other thread.

Print Even : 1
Print Odd : 2
Print Even : 3
Print Odd : 4
Print Even : 5
Print Odd : 6
Print Even : 7
Print Odd : 8
Print Even : 9
Print Odd : 10

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

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...