Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Few questions about synchronized methods and blocks

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading the SCJP 5 book chapter on Threads. To quote from
the synchronization objective

"When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But when you synchronize a block of code, you
specify which object's lock you want to use as the lock, so you could, for example,
use some third-party object as the lock for this piece of code. That gives you the
ability to have more than one lock for code synchronization within a single object."


What does the above mean? I can't understand the part about the synhronized block
in the above paragraph

Also, is the following true

If there is a class which has a synchronized method and a synchronized block,
if a thread enters a synchronized block, no other thread can execute the synchronized
method on the same instance. Just like if a thread enters a synchronized method , no other thread
can execute any synchronized methods on the same instance


Am stuck on this part,
please help to understand

Thanks




 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Rai wrote:"When you synchronize a method, the object used to invoke the method is the
object whose lock must be acquired. But when you synchronize a block of code, you
specify which object's lock you want to use as the lock, so you could, for example,
use some third-party object as the lock for this piece of code. That gives you the
ability to have more than one lock for code synchronization within a single object."


At the risk of verging on the smutty: have you ever used the toilet at a small gas station? Usually, you have to get a key from the cashier, which you return when you're done. This simple procedure prevents you from walking in on someone else while they're doing their business.

Now imagine that the code you want to execute is inside such a restroom. Anyone who wants to execute it has to get the key before they can execute it, and if two people need to use it at the same time, the second person has to wait until the first one is finished.

A slightly larger gas station might have separate toilets for men and women. That means that two people can use the toilets at the same time providing they're of different sexes. A larger one still might have stalls to accommodate more people, but still, only one person at a time will be able to use a particular stall.

It's exactly the same with synchronization: When you synchronize a method you're telling Java that any Thread that wants to execute it has to "get the key" before it can proceed, and all other Threads have to wait unless the first one has returned the key to the cashier.

The only difference between a synchronized method and a synchronized block is that with the latter you tell Java which key you want to use.

Winston
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sounds towards private lock idiom

you can google to get better source ;)
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies but I still have not got the answer to my second question

When we use synchronized block with "this" , i mean current object,
can any other thread enter any other synchronized method(method not block) for that instance.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajiv Rai wrote:can any other thread enter any other synchronized method(method not block) for that instance.


No.

Winston
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic