From Khalid book: pg no.282 WHich of the following statements are true? a) No two threads can ever simultaneously execute synchonized method on the same object. b) Method declared synchonized should not be recursive c) synchonized methods can only call other synchonized methods directly. d) Inside synchonized methods one can be assume that no other threads are currenly executing a method in the same class. My answere a) and d) But given answere is just a) Please can anybody explain?
A class may have synchronized as well as non-synchronized methods. If a thread is executing a synchronized method then that thread has locks on the objects SYNCHRONIZED methods and blocks. So another thread can simultaneously execute any of the NON-SYNCHRONIZED methods of the same object. Hence option #4 is wrong.
Joined: Nov 22, 2008
My understanding of option d) was, when a thread is executing a synchronised method, it is rest asured that no other thread is currently executing that synchronized method at the same point of time. So my answere was true. Or does is it something else. Mean while like you mentioned other threads can execute non-synchronized method. Am I right? Please clarify.