About method calls from a synchronized block

Is method synchronization equivalent, allowing only one thread to evaluate it until it is removed from the scope, including calls to internal methods?

For instance:

public synchronized void foo(){

    if(critical condition){
        bar();  // can influence the above condition
    }
    baz(); // can influence the above condition
}

Could two threads be in bar(suppose it is called only from here)?

What if bazone can call from a different place in the code other than foo, can two threads in it?

+4
source share
2 answers

This is the equivalent way to write code:

public void foo(){
    synchronized (this) {
        if(critical condition){
            bar();  // can influence the above condition
        }
        baz(); // can influence the above condition
    }
}

this .

, foo() bar()? , foo() bar() .

, baz() , baz() , foo().

, Java:

, , , . :

  • .
  • ( )
+2

(, )?

, , - wait() ing.

, baz , foo, ?

, synchronized , .

+3

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


All Articles