Page 1 of 1

About Question enthuware.ocpjp.v8.2.1574 :

Posted: Sat Apr 02, 2016 1:16 am
by schchen2000
The second thread should call getLocks(obj1, obj2)
The 2nd thread can be called without having the need to wait for the 1st thread to finish.

When the 2nd thread calls getLocks(obj1, obj2) and it's assumed that an OS lets it quickly "follow" the 1st thread, it will have to wait for the the 1st thread to finish as the 1st thread goes into getLocks() and both obj1 and obj2 are locked, i.e. the 1st thread "owns" them.

When the 1st thread finishes, it releases both obj1 and obj2 and the 2nd thread will then go in.

Is my understanding correct? Thanks.

Schmichael

P. S.
The second thread should call getLocks() only after first thread exits out of it.
This is also an equally valid way/answer to avoid a deadlock, isn't it?

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Sat Apr 02, 2016 2:08 am
by admin
1. Yes, that is correct.
2. It will avoid a deadlock but it is not the only way to avoid a deadlock.

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Sat Apr 02, 2016 11:37 am
by schchen2000
admin wrote:1. Yes, that is correct.
2. It will avoid a deadlock but it is not the only way to avoid a deadlock.
Thanks a lot. Have a wonderful day to you.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Thu Oct 17, 2019 5:53 pm
by dvc1190
Will there really be a question like this where 2 answers are equally valid, but only one is counted as correct?

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Thu Oct 17, 2019 8:42 pm
by admin
Not equally but yes, you may get questions where two options can be reasonably argued for but you have to select only one. You have to select the best option.

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Fri Oct 18, 2019 8:49 pm
by dvc1190
admin wrote:
Thu Oct 17, 2019 8:42 pm
Not equally but yes, you may get questions where two options can be reasonably argued for but you have to select only one. You have to select the best option.
"Best" is extremely subjective here. Is there a reliable reason to consider the answer to this question as better than the other answer? I don't need your opinion. I need a reason that I can count on using in the actual test.

I know that if I or one of my co-workers came across a method that could cause a deadlock simply by the order or arguments, I'd recommend that the method be synchronized or otherwise made thread safe even it it defeated the purpose of the method. The danger of a deadlock isn't worth the risk. Therefore, the "best" answer is that the second thread is called only after the first thread exits out of it.

Re: About Question enthuware.ocpjp.v8.2.1574 :

Posted: Fri Oct 18, 2019 11:48 pm
by admin
No two options are equally correct in this question so I am not sure what are you confused about. "The second thread should call getLocks() only after first thread exits out of it." is not correct because, based on the given code, there is no way for the second thread to know whether the first thread has exited out of the method.

There is only one correct option and that is option 2 , "The second thread should call getLocks(obj1, obj2)". There is no ambiguity about it. This is the only valid option.

This is not a subjective opinion but a fact.

And yes, you may get a question in the exam where you may feel that two options are equally correct but 1. read the question and the options again, you will most likely find a difference that will let you pick the right option. 2. If you still feel the question has two equally valid options, go with what you feel might be the better one. There is nothing anyone can do about it other that giving your feedback to Oracle about such question. From our experience, we can tell you that such questions are extremely rare.

HTH,
Paul.