`
aahyhaa
  • 浏览: 7897 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

什么时候用到synchronize关键字?

    博客分类:
  • Java
阅读更多

When should you synchronize? Apply Brian’s Rule of Synchronization

If you are writing a variable that might next be read by another thread, or reading a 

variable that might have last been written by another thread, you must use 

synchronization, and further, both the reader and the writer must synchronize using the 

same monitor lock. 

If you have more than one method in your class that deals with the critical data, you must 

synchronize all relevant methods. If you synchronize only one of the methods, then the 

others are free to ignore the object lock and can be called with impunity. This is an important 

point: Every method that accesses a critical shared resource must be synchronized or it 

won’t work right. 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics