• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between synchronized method and block

 
karan wankhade
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Can anyone tell me whats the difference between synchronized method and block, expecting some good logical answers.
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi karan can you please clearly specify your query by explaining which block are you asking for???
 
Norm Radder
Ranch Hand
Posts: 1040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between

[ September 24, 2008: Message edited by: Ramesh Kumar Swarnkar ]
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramesh Kumar Swarnkar:
what is the difference between



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

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic