• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Synchronized with Another object

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the below code snippet taken from http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html



Is it neccesary that the synchronized block member fields and variables must be held by the object used to lock it . i mean is it mandatory that c1 is a member of Dummy class or its parent class(Object class) instance ?

Regards
Luke
 
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, if methods in a particular class are interdependent for state of the object, then it is good to use this/any single object as lock.
If two methods are not dependent (do not get affected by each others operations on the state of the object) then it is not harm to use different locks for the methods. This case is explained in your code.

Here, there is no reason that class used for lock should contain the state variables of synchronized block.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic