• 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

need clarification on the way synchronization works?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
following is paragraph from kb 6 book page no. 735.

With locks. Every object in Java has a built-in lock
that only comes into play when the object has synchronized method code. When
we enter a synchronized non-static method, we automatically acquire the lock
associated with the current instance of the class whose code we're executing (the
this instance).



does this means that if an object does not have synchronized methods, we can't acquire lock on that object. if we can acquire the lock , then how we do that ?

according to me the answer is yes, we can acquire lock on any object in java whether it has synchronized methods or not. we can do so using synchronized blocks. please somebody confirm if this is right ???

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your correct, you can think of the synchronized keyword on a method as simply being like having a synchronised block on the particular object say using "this" surrounding the entire method. I suspect that the book is getting at the fact that you don't need to synchronize on any object if you use the synchronized keyword for a method and by doing this it uses the "built-in lock".

Hope this helps,
Mike
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I think you can lock on any object by using synchronized(obj) {...} , does not matter the object has syn method or not.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic