Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between Synchronized Block and Synchronized Method in Thread ?  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Why java has 2 ways to do synchronization ?

which is best and when to use which one ?

please share your views !

Regards,
prabhat
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method is itself a block of code. So there's no difference between the two things which you mention there.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but my question is

1) public void Synchronized myThread() {


}

2) public void myThread() {

synchronized(this) {

}

}

You don't see any difference between the two ways to sync the threads..

when to use which and why ?

 
Venu Chakravorty
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case the thread acquires the lock on the currently executing object i.e. 'this'. BTW it's 'synchronized' not 'Syn...'.
In the second case you can have code that is both thread safe and thread unsafe (if I may call it so) un the same method, moreover you can specify the object whose lock you want to acquire (by replacing the 'this' keyword with it).
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning". Moving thread.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:

1) public void Synchronized myThread() {


}



just for your interest. it's public synchronized void not public void synchronized ...
 
Vijitha Kumara
Saloon Keeper
Posts: 3997
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:when to use which and why ?

Sometimes to minimize the time the lock is held by a thread you might synchronize a block of code inside the method not the hole method itself.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method is itself a block of code. So there's no difference between the two things which you mention there.


The last time I looked (was a while back) at the generated code the first put a flag on the method (ACC_SYNCHRONIZED) the second had the byte code for a synchronized block which would make it less efficient I guess and I would have said the first was far more maintainable / readable.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:yes but my question is

1) public void Synchronized myThread() {


}

2) public void myThread() {

synchronized(this) {

}

}

You don't see any difference between the two ways to synchroniz the threads..

when to use which and why ?

 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

You type in Google words: concurrency open calls

and all will become clear...

Adam
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the first one unless you have a good reason not to, as you have written it the first one is the winner.

The second one gives you a finer level of control over how long the lock is held within the method and really requires to be more efficient that you have a mix of code that requires to be synchronised and not within the method.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

Or have a look at:
http://www.informit.com/articles/article.aspx?p=167821

and find "open calls" in this article.

Adam
 
suraj august
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized Method it desired when the object or class (resource )is the same class in which the Synchronized method is there .otherwise use Synchronized block to lock on any arbitrary object which is of some other class
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!