Hi,
Question 11 of dan chisholm's mock exam: Threads n�2:
Answer : output
ABC Explanations:
Inside the main method a new thread, t1, is started and will move into the running state at the discretion of the thread scheduler. The A.run method invokes the wait method on the String array object, sa, causing the thread to block until another thread invokes the sa.notify method. Before the B.main method invokes sa.notify all of the elements of the String array object, sa, have already been updated.
BUt as the block
is synchronized, there's no importance if sa.notify() is called before or after updating sa's array. Anyway, we go out of the synchronized block when
all the instructions inside it have been executed So, the following code should give exactly the same result, no?
I tested it, and on my computer, it's the case. But can we generalize?
Thanks for your answer,
Cyril
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287