• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized method and Sychronized block

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between Sychronized method, Synchronizec block and Sychronized statement?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Synchronized method: Only one thread can execute a synchronized method on an object at a time. All other threads have to wait till the execution of that particular thread is completed. This provides locks at method level.

Synchronized statement: Inside the block, all access to a particular object are synchronized. For e.g.



will make sure that i is incremented exactly once by each thread that is trying to do it

_steve.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

1. Synchronized means only one thread can access it.

2. Synchronized keyword is applicable only for method,block or a statement.

3. Synchronized statement : If you are very sure which statement to lock, use this.

4. Synchronized block : If you want block of statements to be protected from concurrent access, use this.

5. Synchronized Method : If you want a functioning method to be blocked from multiple access, use this.

Finally, Synchronization comes into picture while you are using threads.

Thanks,
march
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vallabhaneni Suresh Kumar:
What is the difference between Sychronized method, Synchronizec block and Sychronized statement?



Synchronized methods should never be used as the violate the principle of encapsulation.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pretty strong statement ... what's the rationale and what would you do instead?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
That's a pretty strong statement ... what's the rationale and what would you do instead?



Synchronized methods use publicly accessible variables, 'this' or 'Classname.class'

Instead I would use a synchronized block, with a variable not so publically accessible.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, but the first answer is flat-out wrong.

The difference between a synced block and a synced method is simply one of notational convenience.

That is, syncing an instance method is the same as making the whole method body a block that's synced on this, and syncing a class method is the same as making the whole method body a block that's synced on the associated Class object.

(There's a slight difference in the generated bytecode, due to a bit of a shortcut the VM can take when syncing a method, but functionally they're equivalent.)

I've never heard of a synced statement, but if the term is actually valid, I'd have to suppose that it's like a synced block but with only one statement: perhaps? If so, it's the same as the others--again just a notational convenience to accomplish exactly the same thing over a particular scope.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The difference between a synced block and a synced method is simply one of notational convenience.



I think this is not quite true. Although it can be used like this in the way you describe, a synchronized block is infinitely more useable than a synchronized method. A synchronized method can only synchronize on the object (or class) which holds the method, whereas a synchronized block can synchronize on any Object.

Consider translating this into a synchronized method:



It can't be done. This is the true difference between synchronized blocks and synchronized methods. So, while true that the synchronized method can be a shorthand form of a synchronized block, this is only true in a limited number of cases.
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, every synchronized method can be rewritten as a method with a synchronized block, but not every synchronized block can be rewritten as a synchronized method.

I do partly agree with Gilbert on the fact that it may be safer to NOT use synchronized methods or synchronize on this (or any non-private member) within methods. You are running a risk that someone using your class might not be aware of the synchronization and to synchronize on an instance of your class in a situation that could cause a deadlock.

As far as I can tell, this is less likely to happen if you synchronize on a private data member.

-Yuriy
[ August 10, 2005: Message edited by: Yuriy Zilbergleyt ]
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic