When thread An executes the synchronized method of an object, whether other threads can execute other non-synchronized methods of the object

in the following figure, while thread An executes the synchronized method of obj, thread B can execute other non-synchronized methods of obj

clipboard.png

I have read several blogs on the Internet, and most of them say yes. But as I understand it, when thread A holds a lock on obj, thread B must block access to obj, until it acquires an object lock, right?


the following is a personal guess, if there is any mistake, please correct it!

When the

method is not marked synchronized , it means that you do not need to acquire the object's lock when calling this method. In other words, this call is the same as a normal call, and JVM does not do any special treatment.

if synchronized is marked, the writer thinks that the method may cause a resource conflict, so the writer is required to try to acquire the lock of the object when this method is called and wait until it is successful.

after all, if an object's method does not compete for internal field properties or external resources, it does not matter whether it is called in a multithreaded environment, so JVM gives you the choice to decide whether a method needs to be protected with a lock.

know that in Java , a method is a separate object Method , so just because a method is protected by a lock does not mean that an object instance of the class to which it belongs is protected, unless you explicitly use the synchronized block to lock the object.


answer your own questions!
thread B can of course execute other non-synchronized methods, because there is no synchronized, so the thread does not ask for an object lock to continue execution.

Menu