Page 1 of 1

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

Posted: Wed Jan 02, 2013 5:13 pm
by ETS User
Just small remark
Explanation: This is a complex question. Consider the following situation :
First thread acquires the lock of sb1. It appends X to sb1. Just after that the CPU stops this thread and start the second thread. (Note that first thread still has the lock of sb1). Second thread acquires the lock of sb2 and appends Y to sb2. Now it tries to acquire the lock for sb1. But its lock is already acquired by first thread so the second thread has to wait.
but in code for second thread

Code: Select all

      new Thread
      (
         new Runnable()
         {
            public void run()
            {
                synchronized(sb2)
                {
                    sb1.append("Y"); // <-- here
                    synchronized(sb1)
"Y" is appended to sb1 not to sb2

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

Posted: Thu Jan 03, 2013 7:39 am
by admin
You are right. It should be sb1 in the explanation. This has now been fixed.

thank you for your feedback!