Advantages to using private lock object
来源:互联网 发布:js留言板 编辑:程序博客网 时间:2024/05/29 13:52
There are advantages to using a private lock object instead of an object’s intrinsic lock (or any other publicly accessible lock).
Making the lock object private encapsulates the lock so that client code cannot acquire it, whereas a publicly accessible lock allows client code to participate in its synchronization policy (correctly or incorrectly). Clients that improperly acquire another object’s lock could cause liveness problems, and verifying that a publicly accessible lock is properly used requires examining the entire program rather than a single class.
Good Example:
public class PrivateLock {private final Object myLock = new Object();Widget widget;void someMethod() {synchronized (myLock) {// Access or modify the state of widget}}}
Bad Example:
public class PrivateLock {Widget widget;synchronized void someMethod() {// Access or modify the state of widget}}
"Java concurrency in practice" 4.2.1
Related posts:
Is there any advantage of Java monitor pattern in synchronization
Java Monitor Pattern
- Advantages to using private lock object
- the advantages of using SRL16 to make large delay
- the advantages of using frameworks
- Using public methods to return private data
- bind "xxx.js" to object using script
- Lock Object
- Lock object
- 解决ORA-04021:timeout occurred while waiting to lock object
- ORA-04021:timeout occurred while waiting to lock object ...
- ORA-04021: timeout occurred while waiting to lock object
- [.NET]advantages and disadvantages of using c++ interop
- Using a modal .NET dialog to display AutoCAD object properties
- Using a modeless .NET dialog to display AutoCAD object properties
- How to handle chevron button using CToolBarCtrl object?
- Using Groovy to read values from a different view object
- StuffNet: Using ‘Stuff’ to Improve Object Detection - arxiv 16.10
- when using LOCK TABLES
- Unable to code design using identities in this team: no private keys available
- 简述configure、pkg-config、pkg_config_path三者的关系
- BREW(BUIW)是怎么刷屏的?IDISPLAY_Update()
- 程序员的时间换算表
- 关于C#中对象内存状态的理解
- TortoiseSVN中Branching和Merging实践
- Advantages to using private lock object
- struts2中转发问题
- android中如何利用线程循环打印
- 正则表达式匹配也可以简单快速(上:原理部分)
- [字典树] hdu 1298 poj 1451 T9
- 准备开始看C
- Strut2实现上传
- 如何在软件里显示编译时间?__DATE__和__TIME__
- arm学习b,bl指令浅析