• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Difference between Synchronized Block and Synchronized Method in Thread ?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 26777
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult for "beginning". Moving thread.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic