Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Thu Mar 03, 2016 10:02 pm
by sjsidjain
why option 1 of this question will create deadlock??

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

Posted: Thu Mar 03, 2016 11:11 pm
by admin
Because if you do that two thread will try to acquire the same locks in a different order. You may want to read this for more details: http://tutorials.jenkov.com/java-concur ... ntion.html

HTH,
Paul.

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

Posted: Sun Apr 23, 2023 11:25 am
by steinov
Isn't option 3 also correct? Even though it is overkill, it does avoid a deadlock. Or is it because of the "must"? If that's the case, the phrasing is a bit misleading in my opinion. Is this kind of phrasing something I can expect on the exam?

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

Posted: Sun Apr 23, 2023 12:59 pm
by admin
How will the code ensure that second thread calls getLocks only after the first thread is exits out of it?

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Sun Apr 30, 2023 6:24 am
by steinov
I agree with you that you can't ensure that the getLocks() method is not called before the first thread exits out of it without a locking mechanism or the like. But that doesn't change the fact that it will avoid a deadlock if the second thread would call the getLocks() method after the first thread exits out of it. Even though in practice it would result in some really ugly code, if at all possible. But it wasn't asking about the viability of the option.

Maybe I'm being a bit too nitpicky, but that is how I look at it.

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

Posted: Sat Dec 09, 2023 6:49 am
by imralav
steinov wrote:
Sun Apr 23, 2023 11:25 am
Isn't option 3 also correct? Even though it is overkill, it does avoid a deadlock. Or is it because of the "must"? If that's the case, the phrasing is a bit misleading in my opinion. Is this kind of phrasing something I can expect on the exam?
I agree with the wording being tricky. It could've been worded better, because in my opinion, as it is right now, it should be accepted too.

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Sun Dec 10, 2023 6:23 am
by admin
Option 3 says that second thread "must" call getLocks() only after first thread exits out of it. But that is not true because the second thread can also call getLocks(obj1, obj2) without worrying about the first thread and still deadlock can be prevented.

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Fri Dec 22, 2023 11:22 am
by andrew1584
Question: "Which of the following options avoids a deadlock?"
This option definitely avoids deadlock:
"The second thread must call getLocks() only after first thread exits out of it."

So option 3 is correct.
It wouldn't be correct, if question was "what is the best option" or even "what is true".

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Sat Dec 23, 2023 3:23 am
by admin
Please go through the response above.

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Sat Dec 23, 2023 9:23 am
by andrew1584
Those response is wrong, because it considers answer only, but ignores context(question).
Logically question can be rewritten for every option:
Will this option avoid a deadlock: "The second thread must call getLocks() only after first thread exits out of it."? It is a simple yes/no question and answer "NO" is obviously wrong. So either answer is "Yes" or question has bad wording.

Re: About Question com.enthuware.ets.scjp.v6.2.637 : enthuware.ocpjp.v17.2.1574

Posted: Sat Dec 23, 2023 11:02 am
by admin
If the option had said, "A deadlock can be avoided if the second thread calls getLocks() only after first thread exits out of it", then it would have been correct.

But anyway, since it is causing confusion, we will rephrase it. Although, given that you have to pick only one option, Option 2 is clearly the best option.