This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Synchronization using this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization using this" Watch "Synchronization using this" New topic
Author

Synchronization using this

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
I am not getting when to use synchronized(this) and when it is used
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
GeeCON Prague 2014
 
subject: Synchronization using this