This is from OCP Exam 2 by K and B. The question is number 16. I am not sure why the output includes zero, 5 4 3 2 1 0, when the the boolean test at line 13 says (x > 0)... Is this because there are multiple threads running?
Is the lock referred to in the answer any Stubborn class instance?
Praveen Kumar M K wrote:
Ted North wrote: This is from OCP Exam 2 by K and B. The question is number 16. I am not sure why the output includes zero, 5 4 3 2 1 0, when the the boolean test at line 13 says (x > 0)... Is this because there are multiple threads running?
How about trying without the invocation in line 18. That should answer this question.
Praveen Kumar M K wrote:
Ted North wrote: Is the lock referred to in the answer any Stubborn class instance?
Not any, but the one used in line 10 in the sync block.
Praveen Kumar M K wrote:
This is from OCP Exam 2 by K and B. The question is number 16. I am not sure why the output includes zero, 5 4 3 2 1 0, when the the boolean test at line 13 says (x > 0)... Is this because there are multiple threads running?
How about trying without the invocation in line 18. That should answer this question.
Ted North wrote:
Possible answers from the practice exam:
> A compilation fails
> B The output is 5 4 3 2 1
> C The output is 5 4 3 2 1 0
> D The program could deadlock.
> E The output could be 5, followed by deadlock.
> F If the sleep() invocation was removed, the chance of deadlock would decrease.
> G As it stands, the program can’t deadlock, but if shove() was changed to synchronized, then the program could deadlock.
This is from OCP Exam 2 by K and B. The question is number 16. I am not sure why the output includes zero, 5 4 3 2 1 0, when the the boolean test at line 13 says (x > 0)... Is this because there are multiple threads running?
Also, the answers states: C is correct. It might look like this code could deadlock, but there is only one lock, so no deadlock can occur.
Is the lock referred to in the answer any Stubborn class instance?
Deadlock may occur when two different objects are synchronized.
For example, in shove method:
On the other hand, in push method:
When two threads execute concurrently, one thread calls push and another thread calls shove.
Chances are good that the first thread locks objectB and waits for objectA and the second thread locks objectA (which 1st thread waits for) and waits for objectB (which 2nd thread waits for).
So, in the other words, 1st thread waits for objectA while 2nd thread locks it. The 2nd thread locks objectA , but does not release the lock for the 1st thread because the 2nd thread has not finish the synchronized block.
In your practice exam, the shove method calls push method and it looks like this:
Only the Stuborn.class object is locked. When there is only one object is locked, deadlock won't occur.
Helen Ma wrote:Hi, Tedd
Regard to the zero issue, x-- prints the value of x before x is decreased by 1.
In this example, if t1 locks Stubborn.class object, it keeps calling shove, push, shove, push..... Here are the steps:
1. prints x=5
2. x=4, call push
3. push calls shove
4. prints x= 4
5. x =3, call push and so on.
6. print x = 1
7. x=0, stop
8. t2 locks Stubborn.class object because it is t2's turn now.
9. t2 prints x =0
10. x = -1 and stop
I hope these steps match with what Praveen posted previously.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |