1

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().

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Arman
  • 634
  • 7
  • 12
  • Possible duplicate of [Synchronization and System.out.println](https://stackoverflow.com/questions/9459657/synchronization-and-system-out-println) – Nicholas K Dec 06 '18 at 07:07
  • 1
    Synchronization happens on the instance. When you synchronize a (non-static) method, it's equivalent to wrapping the body in `synchronized (this) {}`. – shmosel Dec 06 '18 at 07:12

1 Answers1

3

In the second example, d1 is a member of the MyThread class that's initialized inline (which is just syntactic sugar over initializing it in the constructor). Each instance of MyThread has its own Display instance, and thus the synchronization is pointless - those are two different objects, synchronizing on two different monitors.

Mureinik
  • 297,002
  • 52
  • 306
  • 350