Inter Thread Communication in Java with wait(),notify(),notifyAll()
Hello everyone,
Today we will talk about notify(),notifyAll() and wait(). Firstly I will make a brief explanation for these three concepts. After that, I will explain on the instance these how it works.
- wait() :Keep the Thread waiting until call notify() or notifyAll() method.When it calls these methods it is wake up again and continue to run
- notify(): After it is work is done, we call the notify() method and this method notify the wait() method and tell this and says that the thread can continue work again.
- notifyAll(): It is wake up all thread in the same class which has wait() method. It is high priority thread that will likely call first but this is not certain.
Note:These method call from object class not thread class and current thread must be synchronize
Differences between wait() and sleep() methods
wait():
1:Comes from the object class
2:This not a static method.
3.It keeps the thread waiting until the call notify() or notifyAll().
Sleep()
1:Comes from Thread class.
2: This is a static method.
3.Wakes up after the specified time.
3:Wakes from sleep after set time .It is doesn't wait for other threads like wait() method.
Apart from the instance, I will give you, you can find a lot of examples on my GitHub account. I put it to the link below here.
Sample Scenario: On the company assign new task employees.When solved task employes set to closed status on the tasks. The first thread pulls new tasks after that it is put on the list. When the number of tasks exceeds 10 it gives the warning this
“The maximum task an employee can take is complete.Please wait for our employee to solve tasks”
and calling the second thread. The Second thread removes an item on the list item with has closed status. When the list will be empty again calling the first thread. Let do write code…
Runnable: This is a @FunctionalInterface. It contains one run method. Please check the below picture
Actually, what we do in TaskManager.java is very simple. We have two Threads: TaskProducer, TaskConsumer.
TaskProducer: TaskProducer adds new items to the list.
TaskConsumer: TaskConsumer removes these items from the list.
as line 45, when the number of the tasks reaches max capacity, it calls the wait() method and keeps the waiting current thread until the calling notifyAll() method. In the thread, the TaskConsumer deletes tasks from the list and called notifyAll() method to notify the other Thread.TaskProducer again add an item to the list and it calls the notifyAll() method and notifies taskConsumer to delete closed tasks.
TaskConsumer also deletes the task from the list until the list is empty. When list.empty() is true it gives the warning
“Please do send new tasks to our employee”
and with the wait() method, the thread stops until the task is added to the list again.
This continues in a cycle.
As you can see in the console below, our tasks are constantly added and deleted.
As you see above, other threads run after warnings are given.
If you want to make the console code more understandable by coloring these, you can use the below class.
This article is over. If there is a point that you don't understand or that you think is incomplete, please comment or contact me from social media accounts.
See you until the next article.