What you are doing is creating a Runnbable
object and making two Thread
object out of it. Assigning two different names to the threads does not make them two threads and conversely, assigning same name to two threads does not make them one thread .
Basically, you are creating a duplicate thread with the same resources.
So, your first thread locks onto the employee
and the second thread (which in reality is the same Runnable
as the first that obtained the lock) requests a lock on employee
. So, it gets blocked.
What is happening is that the Runnable
is requesting a lock on itself.
Just do as Keppil suggested:
Thread t1 = new Thread(new TestClass1(), "T1");
Thread t2 = new Thread(new TestClass1(), "T2");