Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a synchronized method and synchronized block in Java? [duplicate]

What is the difference between a synchronized method and synchronized block in Java ?

I have been searching the answer on the Net, people seem to be so unsure about this one :-(

My take would be there is no difference between the two, except that the synch block might be more localized in scope and hence the lock will be of lesser time ??

And in case of Lock on a static method, on what is the Lock taken ? What is the meaning of a Lock on Class ?

like image 579
Geek Avatar asked Jul 19 '09 13:07

Geek


People also ask

What is the difference between synchronized method and synchronized block in Java?

One classic difference between Synchronized block and Synchronized method is that Synchronized method locks the entire object. Synchronized block just locks the code within the block. Synchronized method: Basically these 2 sync methods disable multithreading.

What is difference between Synchronisation block and Synchronisation method and which is better?

A synchronized method provides a lock corresponding to object-level or Class level ( i.e class level means static method ), whereas, synchronized block provides a lock on any object depending on the parameter.

What is the difference if you use synchronized keyword before a method and a synchronized block?

The main difference between the synchronized method and the synchronized block is a selection of locks on which critical section is locked. A synchronized method depending upon whether it's a static method or non-static locks on either class level lock or object lock.

What is a synchronized method in Java?

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.


2 Answers

A synchronized method uses the method receiver as a lock (i.e. this for non static methods, and the enclosing class for static methods). Synchronized blocks uses the expression as a lock.

So the following two methods are equivalent from locking prospective:

synchronized void mymethod() { ... }  void mymethod() {   synchronized (this) { ... } } 

For static methods, the class will be locked:

class MyClass {   synchronized static mystatic() { ... }    static mystaticeq() {     syncrhonized (MyClass.class) { ... }   } } 

For synchronized blocks, you can use any non-null object as a lock:

synchronized (mymap) {   mymap.put(..., ...); } 

Lock scope

For synchronized methods, the lock will be held throughout the method scope, while in the synchronized block, the lock is held only during that block scope (otherwise known as critical section). In practice, the JVM is permitted to optimize by removing some operations out of the synchronized block execution if it can prove that it can be done safely.

like image 171
notnoop Avatar answered Oct 13 '22 07:10

notnoop


A synchronized method is shorthand. This:

class Something {     public synchronized void doSomething() {         ...     }      public static synchronized void doSomethingStatic() {         ...     } } 

is, for all intents and purposes, equivalent to this:

class Something {     public void doSomething() {         synchronized(this) {             ...         }     }      public static void doSomethingStatic() {         synchronized(Something.class) {             ...         }     } } 

(Where Something.class is the class object for the class Something.)

So indeed, with a synchronized block, you can be more specific about your lock, and more fine-grained about when you want to use it, but other than that there's no difference.

like image 36
jqno Avatar answered Oct 13 '22 06:10

jqno