File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Synchronized method and Sychronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized method and Sychronized block" Watch "Synchronized method and Sychronized block" New topic
Author

Synchronized method and Sychronized block

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
What is the difference between Sychronized method, Synchronizec block and Sychronized statement?
Steve Simon Joseph Fernandez
Ranch Hand

Joined: Jul 17, 2005
Posts: 35
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.
marchrose rose
Greenhorn

Joined: Jul 20, 2005
Posts: 3
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads and Synchronization...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's a pretty strong statement ... what's the rationale and what would you do instead?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
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.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized method and Sychronized block