• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization using this

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting when to use synchronized(this) and when it is used
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh KumarPandey wrote:I am not getting when to use synchronized(this) and when it is used


Any two (or more) threads that should be prevented from working on the same data simultaneously, can (as an option) be synchronized on the same instance. In this regard, synchronized(this) is just a design option. You need to use synchronization to help with thread-safety, and there is no set of rules that says that you have to use the "this" reference for a particular use case.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic