About Question com.enthuware.ets.scjp.v6.2.362 :
Posted: Wed Jan 02, 2013 5:13 pm
Just small remark
"Y" is appended to sb1 not to sb2
but in code for second threadExplanation: 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.
Code: Select all
new Thread
(
new Runnable()
{
public void run()
{
synchronized(sb2)
{
sb1.append("Y"); // <-- here
synchronized(sb1)