You are here:

Java/thread synchronization

Advertisement


Question
Hii! i am trying to understand difference between synchronization block and synchronization method in java.
I searched in google.but i could not get satisfactory and detailed explanation.
can you tell me the difference between them ?
I hope you provide detailed explanation.

Answer
A synchronized method a short way of synchronizing over the body of the method.


public synchronized void method() {
  ...
}

is equivalent too:

public void method() {
  synchronized(this) {
     ...
  }
}

and

public static synchronized void method() {
  ...
}

is equivalent too:

public static void method() {
  synchronized(MyClass.class) { //MyClass is the name of the class the method is in
     ...
  }
}

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.