• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronize a part

 
Anand Sidharth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We synchronize part of a method using the synchronized key by getting lock of an arbitrary object. WHatz the funda behind using an arbitrary object for getting lock.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid there's no quick-and-easy fundamental rule for what you should be synchronizing your blocks of code on. It really depends on what the block of code is actually doing.
If all you need is a simple mutex, then you can synchronize on any dedicated object. If you need something to enforce exclusive access to a resource, then you can synchronize on the resource object. And so forth.
I would say in general, you will synchronize on one of:
+ the class object
+ the instance object (this)
+ a resource
+ an arbitrary object as a mutex
Of course, this is just a generalization. There's no "fundamental" issue here -- you just have to look at your problem and come up with a solution based on what is actually going on.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!