Answer the question
In order to leave comments, you need to log in
What is the difference between a synchronized method and synchronization by object, as well as by class?
In general, the essence of the question is in the title. Please tell me the difference between them. When to use one method or the other. In what ways is one way better than the other and vice versa?
public synchronized void method() {
}
public void method() {
synchronized (object) {
}
}
class C {
public void method() {
synchronized (C.class) {
}
}
}
Answer the question
In order to leave comments, you need to log in
There are two types of synchronized method - static and non-static. Static is equivalent to synchronizing the body of a method on a class:
public synchronized static void method() {
}
class C {
public static void method() {
synchronized (C.class) {
}
}
}
public synchronized void method() {
}
public void method() {
synchronized (this) {
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question