Threading and Sync in Java

If there is a synchronized method in the class and it includes 1 thread, another thread may call the same method for another object.

+3
source share
3 answers

Yes, if the method is not static.

A synchronizednon-static method is synchronized on this. So this method:

public synchronized void foo() {
  // do stuff
}

actually equivalent to this:

public void foo() {
  synchronized(this) {
    // do stuff
  }
}
Synchronized method

A is staticsynchronized with the current class. So this method:

public static synchronized void bar() {
  // do stuff
}

actually equivalent to this:

public static void bar() {
  synchronized(ThisClass.class) {
    // do stuff
  }
}
+7
source

synchronized, . , .
, static one, , [ ]

+3

Yes, another thread can call this method from an instance of this class.

0
source

Source: https://habr.com/ru/post/1795737/


All Articles