The moose likes Threads and Synchronization and the fly likes Synchronized block and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized block and methods" Watch "Synchronized block and methods" New topic

Synchronized block and methods


Joined: Jun 08, 2006
Posts: 24
What are the difference between
Synchronized block and Synchronized methods.
Where we use Synchronized blocks and where we use Synchronized methods
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

Adding the keyword "synchronized" to the method declaration is just a syntactic shortcut. Note that the following two are the same:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ela Jha

Joined: Jul 17, 2006
Posts: 12

This has lock on a single object(this) so all the methods which are synchronized cannot be accessed by another thread

It has lock on an object which can be defined by you let's say a String.
if u have four functions say put,get,push,pop & u want while pushing no thread shud be able to access pop but they can access put or get & vice versa then define single object for push & pop and another object for put & get.Remember lock is on an object
For Example

[ edited to use separate code blocks rather than one big one - the text in between code was treated as an overly long line of code, messing up the display - Jim ]

[ July 17, 2006: Message edited by: Ela Jha ]
[ July 17, 2006: Message edited by: Jim Yingst ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The practical differences are in controlling scope and the monitor.

A synchronized method synchronizes the whole method (no surprise) while a block synchronizes only the block. You might have a longer method with some parts that need synchronized and others that don't. The block lets you sync only the exact lines that really need it.

A synchronized method synchronizes on the object instance or the class. These are very public monitors, meaning some other thread could synchronize on them for the wrong reason, leading to slowdowns or deadlocks. Maybe it's necessary to protect method A from multiple threads and method B from multiple threads, but it's ok if one thread is in A and another is in B. Synchronized methods can't do that, but blocks can by holding different object monitors.

Here's one way to synchronize on a private monitor:

Pop quiz: Why the normally wasteful "new String()" ?
[ July 17, 2006: Message edited by: Stan James ]

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
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
[Stan]: Pop quiz: Why the normally wasteful "new String()" ?

Counterquiz: why a String here at all?

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I couldn't think of a way to give any other Object a meaningful value. "new Anything()" would do the job, but "new Integer(1)" would make readers ask, "Why 1?" Hey, that could be real fun. Let's make it

new ServerSocket("", 1234); // DO NOT CHANGE THIS ADDRESS!
[ July 17, 2006: Message edited by: Stan James ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Personally I think the variable name is the only thing that's going to give it value in this context.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Yeah, I would normally just use

as that raises a minimal number of irrelevant questions. In the unlikely event I need to print something out for debugging purposes, I'd rather have Object's toString(), which will at least print different values for different objects. If I have two different locks that are both Strings that say "lock", I would have a harder time telling them apart than with plain Objects.
I agree. Here's the link: http://aspose.com/file-tools
subject: Synchronized block and methods
Similar Threads
Synchronized blocks vs java.util.concurrent.locks
Synchronized method/blocks.....???????
doubt on Class synchronization ?
synchronized keyword use
Thread question