Sei sulla pagina 1di 4

7/14/13

Difference between synchronized method and synchrozed block (Java in General forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Java in General

Author

Difference between synchronized method and synchrozed block


posted 11/4/2009 12:45:03 PM

Anwar Hussain Ranch Hand Joined: Jul 24, 2009 Posts: 35

Hi, Can some body explain the difference between synchronized method and synchrozed block. Thanks

Patricia Samuel Ranch Hand Joined: Sep 12, 2007 Posts: 300

posted 11/4/2009 1:08:12 PM

See it here

Anwar Hussain Ranch Hand Joined: Jul 24, 2009 Posts: 35

posted 11/4/2009 1:18:57 PM


Patricia Samuel wrote:

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

posted 11/4/2009 1:23:26 PM

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

print

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.

Anwar Hussain Ranch Hand Joined: Jul 24, 2009 Posts: 35

posted 11/4/2009 2:06:59 PM


Patricia Samuel wrote:

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

print

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

posted 11/4/2009 4:49:35 PM


A nwar Hussain wrote:

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.

Vivek Singh Ranch Hand Joined: Oct 27, 2009 Posts: 92

posted 11/4/2009 5:03:54 PM


A nwar Hussain wrote:

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

print

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 " ) ; }

Is equivalent to :view plain c opy to c lipboard print ?

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."

I agree. Here's the link: http://aspose.com/file-tools

subject: Difference between synchronized method and synchrozed block

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/469462/java/java/Difference-synchronized-method-synchrozed-block

4/4

Potrebbero piacerti anche