Locking concept doubt:
Thread calling a method on an instance even if another thread has a lock on that same instance ?
I have a Student class with 2 non-static methods, one synchronized and another non-synchronized method.
I am creating 2 threads (Thread_A & Thread_B) using the same instance of the Student class.
Thread_A will always call the synchronized method and Thread_B will always call the non-synchronized method.
Suppose Thread_A starts before Thread_B and enters the synchronized method. Now Thread_A has acquired a lock on the instance of the Student class and will not realease it untill it finishes the execution. My doubt is that even if the Thread_A has a lock on the student instance, why Thread_B is able to call the Non-Sync method on the same student instance before Thread_A releases the lock on that instance.
Why locking works only if both methods are synchronized.
Output: