- Notification to number of threads : We can use notify() method to give the notification for only one thread which is waiting for a particular object whereas by the help of notifyAll() methods we can give the notification to all waiting threads of a particular object.
- Notifying a thread by JVM : If multiple threads are waiting for the notification and we use notify() method then only one thread get the notification and the remaining thread have to wait for further notification. Which thread will get the notification we can’t expect because it totally depends upon the JVM. But when we use notifyAll() method then multiple threads got the notification but execution of threads will be performed one by one because thread requires lock and only one lock is available for one object.
- Interchangeability of threads : We should go for notify() if all your waiting threads are interchangeable (the order they wake up doesn’t matter). A common example is a thread pool. But we should use notifyAll() for other cases where the waiting threads may have different purposes and should be able to run concurrently. An example is a maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource.
When to use notify() method and notifyAll()
- In case of mutually exclusive locking, only one of the waiting threads can do something useful after being notified (in this case acquire the lock). In such a case, you would rather use notify(). Properly implemented, you could use notifyAll() in this situation as well, but you would unnecessarily wake threads that can’t do anything anyway.
- In some cases, all waiting threads can take useful action once the wait finishes. An example would be a set of threads waiting for a certain task to finish; once the task has finished, all waiting threads can continue with their business. In such a case you would use notifyAll() to wake up all waiting threads at the same time.
Applications of notify() and notifyAll()
- A maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource; for these we should go for notifyAll().
- Let’s say we have a producer thread and a consumer thread. Each “packet” produced by the producer should be consumed by a consumer. The consumer puts something in a queue and then calls notify().
- We want to have a notification when a lengthy process has finished. You want a beep and a screen update. The process performs notifyAll() to notify both the beeping-thread and the screen-update-thread.
Reference : https://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again
相关推荐
Differences between C++ and C# 可以看看。
Differences between EEWB and BDT
Talk Show Differences Between China and America from the Perspective of the Form and Express.zip
however, there are some significant differences between the capabilities of Silverlight and WPF, as well as important differences in the programming features of the two technologies. There are ...
The Differences Between Chinese and American Cultures from the Perspective of Politeness Utterances.zip
What are the differences between least-squares and Kalman filtering
Differences Between S32K11x and S32K142,11x系列与K142的不同
state 50 differences between marketing and sales
英语和汉语的区别
飞思卡尔为处理器IMX51与IMX53的区别
Employment-related differences between male and female school psychologists Psychulony in Ihe Schools Yollrme 31. July /994 EMPLOYMENT-RELATED DIFFERENCES BETWEEN MALE AND FEMALE SCHOOL ...
Cultural Differences in Diets Between China and the West.zip
Differences between urban and rural school psychology: Training implications P \ i i h o l o ~ i in rhr Srhvolr I Y X I . l R . 191-196 DIFFERENCES BETWEEN URBAN AND RURAL SCHOOL PSYCHOLOGY: ...
SQL相关资料
Exercise 2.1: What are the differences between int, long, long long, and short? Between an unsigned and a signed type? Between a float and a double? Exercise 2.2: To calculate a mortgage payment, what...
Diagnostic differences between educationally handicapped and learning disabled students Pr~.rhulugv in the Schuul.r PPVT and MSCA with Autistic Children 469 1980. 17. 469-473 SCHOPLER, E. ...
The Differences of Chinese and English中英文写作差异
Identification of statistically significant differences between scaled scores and psycholinguistic ages on the ITPA IDENTIFICATION OF STATISTICALIiY SIGNIFICANT DIFFERENCES BETWEEN SCALED SCORES ...
数据库系统概念课后习题答案
Summary: <br/>Symptom: This is an explanation of differences between TYPE and LIKE additions <br/>Additional key words: Data types, data objects, ABAP type concept, Dictionary ...