- 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
相关推荐
C++与C#是两种广泛应用的编程语言,它们在很多方面都有显著的区别。本文将详细探讨这两种语言之间的主要差异,帮助开发者理解它们各自的特点。 1. 命名空间和成员访问: - C# 使用点号(.)来分隔命名空间、类以及...
Talk Show Differences Between China and America from the Perspective of the Form and Express.zip
The Differences Between Chinese and American Cultures from the Perspective of Politeness Utterances.zip
在IT领域,尤其是在企业软件开发中,理解不同的增强技术至关重要。本文将深入探讨EEWB(Enhancement Element Workbench)和BDT(Business Data Type)之间的差异,这两种工具都是用于SAP CRM系统中的对象扩展和定制...
英语与汉语的语言结构差异是一个复杂且广泛的话题,涉及语言学的多个层面。首先需要明确的是,任何两种语言之间的结构差异都是各自文化和思维模式差异的反映。这一点在英汉语言对比中尤为明显。...
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 ...
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: ...
### 最小二乘法与卡尔曼滤波的区别 在GPS导航系统和其他位置定位技术中,两种最常用的估计方法是最小二乘法(Least Squares, LS)和卡尔曼滤波(Kalman Filtering, KF)。这两种方法虽然表面上看起来有很大区别,但...
【标题】:初识SQL:SQL Server与Oracle之间的差异 【描述】:这份资料主要针对SQL初学者,尤其是从SQL Server环境转向Oracle的用户,详细介绍了两者在语法、功能和概念上的不同之处。 【标签】:SQL ...
S32K11x和S32K142微控制器(MCUs)是NXP半导体公司生产的两个系列的产品。虽然两者具有许多共通模块,但也存在一些显著差异,尤其是在核心架构、内存选项、外设配置、中断映射和软件迁移的兼容性等方面。...
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. ...
Differences-between-LIN20-and-LIN21 (1)
Identification of statistically significant differences between scaled scores and psycholinguistic ages on the ITPA IDENTIFICATION OF STATISTICALIiY SIGNIFICANT DIFFERENCES BETWEEN SCALED SCORES ...
数据库系统概念课后习题答案
Philosophical differences between early childhood education and special education: Issues for school psychologists Psychology in Ihe Schools Volume 23, April 1986 PHILOSOPHICAL DIFFERENCES ...
《东西方饮食文化差异》 饮食,作为人类生活的基本需求之一,不仅是生理的满足,更是文化的体现。中国和西方在饮食习惯、食材选择、烹饪方式等方面存在着显著的文化差异,这些差异背后反映了各自的历史传统、哲学...
Significant differences between individual subtest scaled scores and average scaled scores on the Wechsler Preschool and Primary Scale of Intelligence-Revised Psychology in the Schools Volume 28. ...
Reliability and abnormality of differences between DTVP subtest scores 204 JAMES E. YSSELDYKE, DAVID A. SABATINO AND JOSEPH LAMANNA CONCLUSION A multitrait-multimethod matrix was used to ...