Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.362 :

Posted: Thu Nov 14, 2013 10:18 am
by patpat09
There is a small mistake in the explanation:

Code snip:

Code: Select all

public class TestClass
{
  static StringBuffer sb1 = new StringBuffer();
  static StringBuffer sb2 = new StringBuffer();
  public static void main(String[] args)
  {
      new Thread
      (
         new Runnable()
         {
            public void run()
            {
                synchronized(sb1)
                {
                    sb1.append("X");
                    synchronized(sb2)
                    {
                      sb2.append("Y");
                    }
                }
                System.out.println(sb1);
            }
         }
      ).start();
      new Thread
      (
         new Runnable()
         {
            public void run()
            {
                synchronized(sb2)
                {
                   sb1.append("Y"); //<- appends Y to sb1, not "appends Y to sb2"
                    synchronized(sb1)
                    {
                      sb2.append("X"); // <- appends X to sb2
                    }
                }
               System.out.println(sb2);
            }
         }
      ).start();
  }
}

Explanation:
[...] Second thread acquires the lock of sb2 and appends Y to sb2.[...]
I think it must correct to:

Re: About Question com.enthuware.ets.scjp.v6.2.362 :

Posted: Thu Nov 14, 2013 7:58 pm
by admin
Fixed.
thank you for your feedback!
Paul.