Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Differrence Between Synchronized Method and Synchronized Block  RSS feed

 
Md. Hasibuzzaman Chowdhury
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, Hope you are going well . I am studying Multithreading. I am confused what the difference between Synchronized Method and Synchronized Block.

I know that Synchronized method Lock on the Object when called. But confusion about synchronized Block , if we call a member method/variable of a normal class from a Synchronized Block , Does the Synchronized Block Lock on the Object or just lock on the method/variable only , so that other member method/variables can be used at the same time, Or It also Block on the Object as Lock Synchronized method ?

Please help me to get the thing clearly . Thank you very much
 
Stephan van Hulst
Saloon Keeper
Posts: 7713
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two are exactly the same:

Synchronizing a method acts as if the entire method body is a synchronized block, locking on the current object.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of a synchronised method, the object holding the lock is by default “this” (for static methods whatever getClasss() would return if you you could call getClass() from a static method).
In the case of synchronised bocks you specify the object to hold the lock.Duplicating discussion in our threads forum
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Synchronizing a method acts as if the entire method body is a synchronized block, locking on the current object.

@Md. Hasibuzzaman: There is one slight advantage to synchronizing methods though - it declares intent.

If you use javadoc (and if you don't, I suggest you start ) the fact that a method is synchronized will show up in its documentation, so everybody will know that you only intended it to be run by one thread at a time.

HIH

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7713
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If you use javadoc (and if you don't, I suggest you start ) the fact that a method is synchronized will show up in its documentation, so everybody will know that you only intended it to be run by one thread at a time.


It's important to note that this is an implementation detail. I'm actually not sure why they chose to include it in the JavaDoc. If a class or method is safe to use by multiple threads, it should be explicitly documented.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:[It's important to note that this is an implementation detail. I'm actually not sure why they chose to include it in the JavaDoc. If a class or method is safe to use by multiple threads, it should be explicitly documented.

I disagree. Why or how an object or method is synchronized is an implementation detail, but that it is is not.
And what's more explicit than showing the method signature?

Winston
 
Md. Hasibuzzaman Chowdhury
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody for helping me .

But actually my query was :

I know that Synchronized method Lock on the Object when called. But confusion about synchronized Block , if we call a member method/variable of a normal class from a Synchronized Block , Does the Synchronized Block Lock on the Object or just lock on the method/variable only , so that other member method/variables can be used at the same time, Or It also Block on the Object as Lock Synchronized method ?

Can you please help me to clear about this ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7713
141
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I disagree. Why or how an object or method is synchronized is an implementation detail, but that it is is not.
And what's more explicit than showing the method signature?

The synchronized keyword is not part of the method signature. If so, subclasses that override a synchronized method would need to be synchronized as well and you would also be able to overload a synchronized method with an unsynchronized version.

Thread safety is the that. synchronized is the how.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Md. Hasibuzzaman Chowdhury wrote:Does the Synchronized Block Lock on the Object or just lock on the method/variable only , so that other member method/variables can be used at the same time, Or It also Block on the Object as Lock Synchronized method ?

It locks on whatever object you name in the synchronized(...) bit, and it releases it as soon as the block is finished.

It's basically a construct that allows you to protect a smaller amount of code than a whole method, or lock it on a different object to the "current" one (ie, this).

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The synchronized keyword is not part of the method signature.

OK, method declaration then - ie, what javadoc shows you. Are you saying it shouldn't show that at all, or that it should pick and choose what it does?

Thread safety is the that. synchronized is the how.

OK again; but back in the days before ReentrantLocks, it used to be (basically) the only "how", which is why synchronized is a keyword.

And what are you going to say in any explicit documentation that isn't conveyed by showing it?

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7713
141
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Md. Hasibuzzaman Chowdhury wrote:I know that Synchronized method Lock on the Object when called. But confusion about synchronized Block , if we call a member method/variable of a normal class from a Synchronized Block , Does the Synchronized Block Lock on the Object or just lock on the method/variable only , so that other member method/variables can be used at the same time, Or It also Block on the Object as Lock Synchronized method ?


A synchronized block locks only on the object that you specified in the header of the block. Locking on an object doesn't block access to its members. The only thing locking does is block other threads from locking on the same object:

In the code above, if both methods are run at the same time, they can both run a.doSomething() at the same time, because they lock on different objects (assuming a and b hold different object references). If you want to properly synchronize two blocks of code, they need to lock on the same object, even if that object has nothing to do with the code you want to run.

Here, even though a and b refer to different objects and different calls are performed on both, when foo() and bar() run at the same time, they can't perform the actions on their respective data because they lock on the same object.
 
Stephan van Hulst
Saloon Keeper
Posts: 7713
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:OK, method declaration then - ie, what javadoc shows you. Are you saying it shouldn't show that at all, or that it should pick and choose what it does?

I'm saying it shouldn't be shown at all. javadoc also doesn't show the method body. Actually, I just read somewhere that since javadoc 1.2, the synchronized keyword doesn't show up anymore.

OK again; but back in the days before ReentrantLocks, it used to be (basically) the only "how". And what are you going to say in any explicit documentation that isn't conveyed by the word 'synchronized'?

The same you would say if a method was safe to use if you didn't use the synchronized keyword: "This method is thread-safe". For an example, check Throwable.addSuppressed().
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Actually, I just read somewhere that since javadoc 1.2, the synchronized keyword doesn't show up anymore.

Ooof. It would appear you're right. I just looked at StringBuffer.

'Fraid I can't agree with either you or javadoc on this one - and I particularly don't like that fact that they've made a change which removes information. Very cavalier IMO.

Winston
 
Md. Hasibuzzaman Chowdhury
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much all of you for helping me specially to Winston Gutkowski, I get my answer from Winston reply . God bless you . have a nice day

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Md. Hasibuzzaman Chowdhury wrote:Thank you very much all of you for helping me specially to Winston Gutkowski, I get my answer from Winston reply.

That's a first, but you're most welcome. I hope the other banter didn't distract you too much.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!