File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized method and Sychronized block

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between Sychronized method, Synchronizec block and Sychronized statement?
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
marchrose rose
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic