class Demo
{
void demo()
{
System.out.println(\"Inside demo of \"+Thread.currentThread().getName());
try
{
Thread.sleep(10000
So, when MyThread1 is executing, due to the
synchronized
block, the monitor ofd
should be locked, resulting in denial of access tod.demo()
by the MyThread2.
That would only happen if MyThread2 also had a synchronized
block. When one thread is synchronized on an object, other threads will be blocked if they also try to synchronize on that same object. If they don't synchronize, they won't be. There's nothing that stops access to an object from threads that don't synchronize on it.
Synchronization is a cooperative mechanism. It only works when all threads work together.