aspose file tools*
The moose likes Threads and Synchronization and the fly likes difference between synchronized method and block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "difference between synchronized method and block" Watch "difference between synchronized method and block" New topic
Author

difference between synchronized method and block

karan wankhade
Greenhorn

Joined: Aug 25, 2008
Posts: 4
hi,
Can anyone tell me whats the difference between synchronized method and block, expecting some good logical answers.
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

hi karan can you please clearly specify your query by explaining which block are you asking for???
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
The syntax says it all. For a method it must synch on the whole class.
For a block it only synchs on the referenced object, the class containing the block is not locked.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There are several key differences:

1) synchronized code always use objects as locks to prevent other threads from entering the synchronized block. For instance methods, they are synchronized on the 'this' reference, and for static methods they are synchronized on the instance of the Class object method belongs to. In blocks if code using the synchronized(object) {} syntax you can use any object as the lock

2) Because of 1) you have a lot more granularity with the synchronized(object) {} syntax rather than synchronized method. You can have different methods synchronize on different objects, or different parts of a method synchronized on different objects.

3) Best practice is to minimize the code inside synchronized blocks because they prevent concurrent threads from executing - losing any advantage of multi-threaded applications. Synchronized blocks help let those portions of a method that do not access shared resources to be run simultaneously while still keeping those parts that need to be shared thread-safe.

To combine 2 and 3, lets setup this scenario:

You have a Customer which has a Shopping Cart and some Recommendations. Your client is allowed to have multiple threads operating on the Customer at once. You have three methods for accessing data to the customer:

In this scenario, you couldn't add an item to the cart in one thread while adding recommendations in another thread, even though they don't access the same shared data because you are synchronizing on the Customer object. But if you changed it to:

Now you can change recommendations while modifying/displaying the cart and vice-versa and so run more code in parallel.


Steve
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
what is the difference between

[ September 24, 2008: Message edited by: Ramesh Kumar Swarnkar ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

Originally posted by Ramesh Kumar Swarnkar:
what is the difference between



The first one will compile. The second one won't.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between synchronized method and block