Difference Between Synchronized Method and Block (Threads Forum at JavaRanch)
Difference Between Synchronized Method and Block (Threads Forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
Author
hi, Can anyone tell me whats the difference between synchronized method and block, expecting some good logical answers.
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: 681
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.
https://www.coderanch.com/t/234351/threads/java/difference-synchronized-method-block
1/4
7/14/13
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 multithreaded 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:
view plain c opy to c lipboard print ?
6
I like...
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 .
c l a s sC u s t o m e r{ L i s tr e c o m m e n d a t i o n s ; S h o p p i n g C a r tc a r t ; p u b l i cs y n c h r o n i z e dv o i da d d R e c o m m e n d a t i o n ( . . . ){ / / c h a n g er e c o m m e n d a t i o n s } p u b l i cs y n c h r o n i z e dv o i da d d I t e m T o C a r t ( . . . ) { / / c h a n g eS h o p i n g C a r t } / *d i s p l a y sc a r ta n ds h o w sr e c o m m e n d e dn e x tp u r c h a s e s* / p u b l i cs y n c h r o n i z e dv o i dd i s p l a y C a r t ( ){ / / d i s p l a ys h o p p i n gc a r t / / d i s p l a yr e c o m m e n d a t i o n s } }
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:
view plain c opy to c lipboard print ?
https://www.coderanch.com/t/234351/threads/java/difference-synchronized-method-block
2/4
7/14/13
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 .
c l a s sC u s t o m e r{ p r i v a t eL i s tr e c o m m e n d a t i o n s ; p r i v a t eS h o p p i n g C a r tc a r t ; p r i v a t eO b j e c tr e c o m L o c k ; p r i v a t eO b j e c tc a r t L o c k ; p u b l i cv o i da d d R e c o m m e n d a t i o n ( . . . ){ s y n c h r o n i z e d( r e c o m L o c k ){ / / c h a n g er e c o m m e n d a t i o n s } } p u b l i cv o i da d d I t e m T o C a r t ( . . . ) { s y n c h r o n i z e d( c a r t L o c k ){ / / c h a n g eS h o p p i n g C a r t } } / *d i s p l a y sc a r ta n ds h o w sr e c o m m e n d e dn e x tp u r c h a s e s* / p u b l i cv o i dd i s p l a y C a r t ( ){ s y n c h r o n i z e d( c a r t L o c k ){ / / d i s p l a yS h o p p i n g C a r t } s y n c h r o n i z e d( r e c o m L o c k ){ / / d i s p l a yr e c o m m e n d a t i o n s } } }
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: 83
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
s y n c h r o n i z e d( o b j e c t ){ / / c h a n g eS h o p p i n g C a r t } a n d s y n c h r o n i z e d{ / / c h a n g eS h o p p i n g C a r t }
https://www.coderanch.com/t/234351/threads/java/difference-synchronized-method-block
7/14/13
Sheriff Joined: Sep 28, 2004 Posts: 17009 Originally posted by Ramesh Kumar Swarnkar: what is the difference between
view plain c opy to c lipboard print ?
21
I like...
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
s y n c h r o n i z e d( o b j e c t ){ / / c h a n g eS h o p p i n g C a r t } a n d s y n c h r o n i z e d{ / / c h a n g eS h o p p i n g C a r t }
The first one will compile. The second one won't. Henry
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Similar Threads Synchronized Block and Synchronized method Synchroniztion Threads static synchronized block Difference between synchronized method and synchrozed block
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:48:22 .
https://www.coderanch.com/t/234351/threads/java/difference-synchronized-method-block
4/4