This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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
 
Marshal
Posts: 28000
94
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: 78675
374
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • 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
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic