Regarding synchronized methods in Java

I have a question about method synchronization in Java.

Consider a class with 3 synchronizedmethods.

class MyClass{

    public synchronized void methodA(){ ... }

    public synchronized void methodB(){ ... }

    public synchronized void methodC(){ ... }

}

Consider an myObjectinstance myClass. Which of the following is true?

Option 1:

It is not possible for a thread to execute any synchronized method in myObject, while any synchronized method in is running in another thread myObject.

For example, although thread 1 runs an methodA()instance myObject, thread 2 cannot run any methods methodA(), methodB()and methodC().

Option 2:

It is not possible for a thread to execute a specific method synchronizedin myObject, and this particular method is started by another thread.

, 1 methodA() myObject, 2 methodA(), methodB() methodC(), (, ")".

+4
1

- , .

+3

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


All Articles