Synchronized Block v.s. Synchronized Method

J

Jerry

Anyone knows the differences between synchronized block and synchronized method?

Thanks!
 
A

Andrew Thompson

T

Tor Iver Wilhelmsen

Anyone knows the differences between synchronized block and synchronized method?

A synchronized method is a method with an implicit

synchronized (this) {

}

around all statements.

(or synchronized(MyClass.class) for static methods)

The difference is that an explicit synchronized block can span any
number of method statements, and can use any object for synchronizing
on.
 
Joined
Nov 3, 2007
Messages
1
Reaction score
0
Synchronized block Vs Synchronized method

First of all to achieve Multithreading mechanism in java we should go for synchronization. And this can be done in two ways depending on the requirement.

1. Synchronized block and
2. Synchronized method.

if you go for synchronized block it will lock a specific object.

if you go for synchronized method it will lock all the objects.

in other way Both the synchronized method and block are used to acquires the lock for an object. But the context may vary. Suppose if we want to invoke a critical method which is in a class whose access is not available then synchronized block is used. Otherwise synchronized method can be used.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc

For a detail clarification see the below code

for example:
//Synchronized block

class A
{ public void method1() {...} }
class B
{
public static void main(String s[])
{ A objecta=new A();
A objectb=new A();
synchronized(objecta){objecta.method1();}
objectb.method1(); //not synchronized
}
}


//synchronized method
class A
{ public synchronized void method1() { ...}
}
class B
{
public static void main(String s[])
{
A objecta=new A();
A objectb =new A();
objecta.method1(); objectb.method2();

}
}



Moti Lal D
 
Joined
Aug 11, 2010
Messages
1
Reaction score
0
Synchronized blocks

nicemothi said:
Synchronized methods are used when we are sure all instance will work on the same set of data through the same function Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc

Synchronized blocks are quite useful also for many other situations. For example you can prevent a nested monitor problem where the code would otherwise go deadlock. In general they are a good way to separate code that needs synchronization from other stuff, which can sometimes speed up the program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top