Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Difference between synchronized method and synchrozed block (Java in General forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
Author
Hi, Can some body explain the difference between synchronized method and synchrozed block. Thanks
Patricia Samuel Ranch Hand Joined: Sep 12, 2007 Posts: 300
See it here
See it here
https://www.coderanch.com/t/469462/java/java/Difference-synchronized-method-synchrozed-block
1/4
7/14/13
Difference between synchronized method and synchrozed block (Java in General forum at JavaRanch)
Its confusing me
Patricia Samuel Ranch Hand Joined: Sep 12, 2007 Posts: 300
with a synchronized block you can choose what to synchronize on. With a synchronized method, an instance method always synchronizes on the instance, a static method always synchronizes on the class.
view plain
c opy to c lipboard
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 .
p u b l i cs y n c h r o n i z e dv o i dm e t h o d N a m e ( ){ / /. . . c o d eh e r e } p u b l i cv o i dm e t h o d N a m e ( ){ s y n c h r o n i z e d( t h i s ){ / /. . . c o d eh e r e } }
synchronized(this) can have any object you want to synchronize where as when a method is synchronized it will work with instance for which it is called.
with a synchronized block you can choose what to synchronize on. With a synchronized method, an instance method always synchronizes on the instance, a static method always synchronizes on the class.
view plain
c opy to c lipboard
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 .
p u b l i cs y n c h r o n i z e dv o i dm e t h o d N a m e ( ){ / /. . . c o d eh e r e } p u b l i cv o i dm e t h o d N a m e ( ){ s y n c h r o n i z e d( t h i s ){ / /. . . c o d eh e r e } }
synchronized(this) can have any object you want to synchronize where as when a method is synchronized it will work with instance for which it is called.
https://www.coderanch.com/t/469462/java/java/Difference-synchronized-method-synchrozed-block
2/4
7/14/13
Difference between synchronized method and synchrozed block (Java in General forum at JavaRanch)
So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???
Embla Tingeling Ranch Hand Joined: Oct 22, 2009 Posts: 237
So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???
It doesn't matter whether a whole method is synchronized or just a block. Synchronized always means the synchronized code section can be run by one thread at a time only.
So when a thread is in the middle of execution of the synchronized block with 'this' as argument , can another thread call a synchronzed method of that same object???
view plain
c opy to c lipboard
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 .
p u b l i cs y n c h r o n i z e dv o i dd o S t u f f( ){ S y s t e m . o u t . p r i n t l n ( " s y n c h r o n i z e d " ) ; }
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 .
p u b l i cv o i dd o S t u f f ( ){ s y n c h r o n i z e d ( t h i s ){ S y s t e m . o u t . p r i n t l n ( " s y n c h r o n i z e d " ) ; } }
Form K&B "When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code,
https://www.coderanch.com/t/469462/java/java/Difference-synchronized-method-synchrozed-block 3/4
7/14/13
Difference between synchronized method and synchrozed block (Java in General forum at JavaRanch)
you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object."
Similar Threads Synchronized Block and Synchronized method Synchroniztion Threads difference between synchronized method and block static synchronized 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:46:54 .
https://www.coderanch.com/t/469462/java/java/Difference-synchronized-method-synchrozed-block
4/4