Java Multithreading Inter-Thread Communication | wait(), notify() and notifyAll() Method|print num
Автор: CS-unlocker
Загружено: 2025-09-25
Просмотров: 19
Описание:
A tutorial on wait and notify; low-level thread synchronization methods in Java
Java Thread wait , notify and notifyAll used for inter thread communication via accessing common object.
Object class has three final method called wait, notify and notifyAll. These methods are used for inter thread communication.
#javaprogramming #javamultithreading #programming #javathreads #javaconcepts #javaforbeginners
Note:
1. These methods need to be called from synchronized context,otherwise it will throw java.lang.IllegalMonitorStateException.
2. Only one thread can enter at a time in synchronized block
3. A thread required lock on the object to enter in synchronized block.
4. If Thread T1 want to enter in synchronized block then Thread T1 has to wait for Thread T2 to release it.
Methods:
1. wait():
When you call wait method on the object then it tell threads to give up the lock and go to sleep state unless and until some other thread enters in same monitor and calls notify or notifyAll methods on it.
2. notify():
When you call notify method on the object, it wakes one of thread waiting for that object
3. notifyAll() :
notifyAll will wake up all threads waiting on that object
USE:
JAVA: How to print sequence of number(natural order up to MAX using 3 threads.
For example:
Let’s say you have 3 threads. T1,T2 and T3.
Solution:
use concept of remainder here.
If number%3==1 T1 will print the number and increment it, else will go in the wait state.
If number%3==2 T2 will print the number and increment it else will go in the wait state.
If number%3==0 T3 will print the number and increment it else will go in the wait state.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: