• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic