As per my code, I have three classes:
Display:
class Display
{
synchronized public void wish(String name)
{
for(int i = 0; i < 3; i++)
{
System.out.print("Good Morning: ");
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
}
System.out.println(name);
}
}
}
MyThread:
class MyThread extends Thread
{
String name;
Display d;
MyThread(Display d, String name)
{
this.name = name;
this.d = d;
}
@Override
public void run()
{
d.wish(name);
}
}
Demo:
class SynchronizedDemo
{
public static void main(String[] args)
{
Display d = new Display();
MyThread mt1 = new MyThread(d, "foo");
MyThread mt2 = new MyThread(d, "bar");
mt1.start();
mt2.start();
}
}
It's working fine when I run the Demo class and I get the following output:
Good Morning: bar
Good Morning: bar
Good Morning: bar
Good Morning: foo
Good Morning: foo
Good Morning: foo
Output completed (6 sec consumed) - Normal Termination
Here I totally understand the fact that sleep() method will not release the object lock so once a thread gets into the wish() method, it finishes its work, then only another thread may get the chance.
But if I change my MyThread class as follows:
class MyThread extends Thread
{
String name;
Display d;
MyThread(Display d, String name)
{
this.name = name;
this.d = d;
}
Display d1 = new Display();
@Override
public void run()
{
d1.wish(name);
}
}
Now, the only change is that I am using Display class object that I have initialized inside MyThread class. But the out was totally unexpected to me:
Good Morning: Good Morning: foo
Good Morning: bar
Good Morning: bar
Good Morning: foo
Good Morning: bar
foo
Output completed (3 sec consumed) - Normal Termination
Here it can clearly be understood with the output that as soon as one thread goes on sleep, other is getting the chance to execute wish().