Skip to main content
deleted 3 characters in body
Source Link
atamanroman
  • 11.9k
  • 8
  • 61
  • 83
static synchronized void test() { foo(); } 

equals

static void test() { synchronized(MyClass.class) { foo(); } } 

while

synchronized void test() { foo(); } 

equals

void test() { synchronized(this) { foo(); } } 

This means: While static methods lock on the class object of the class, non. Non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock)synchronized(anyOtherLock) is also possible). Since they lock on different objects, they can run in "parallel"don't block each other.

static synchronized void test() { foo(); } 

equals

static void test() { synchronized(MyClass.class) { foo(); } } 

while

synchronized void test() { foo(); } 

equals

void test() { synchronized(this) { foo(); } } 

This means: While static methods lock on the class object of the class, non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock) is also possible). Since they lock on different objects, they can run in "parallel".

static synchronized void test() { foo(); } 

equals

static void test() { synchronized(MyClass.class) { foo(); } } 

while

synchronized void test() { foo(); } 

equals

void test() { synchronized(this) { foo(); } } 

This means: static methods lock on the class object of the class. Non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock) is also possible). Since they lock on different objects, they don't block each other.

Source Link
atamanroman
  • 11.9k
  • 8
  • 61
  • 83

static synchronized void test() { foo(); } 

equals

static void test() { synchronized(MyClass.class) { foo(); } } 

while

synchronized void test() { foo(); } 

equals

void test() { synchronized(this) { foo(); } } 

This means: While static methods lock on the class object of the class, non-static methods lock on the instance on which they're called (by default, synchronized(anyOtherLock) is also possible). Since they lock on different objects, they can run in "parallel".