Page 1 of 1
About Question enthuware.ocpjp.v7.2.1338
Posted: Wed Feb 25, 2015 9:15 am
by ThufirHawat
I think the answer is wrong:
"When there is more than one thread waiting to obtain the monitor of an object, there is no way to be sure which thread will be notified by the notify() method. "
Logically, if there is "more than one thread waiting to obtain the monitor", THERE IS NO thread waiting yet, but holding to get the monitor to AFTER it can wait. If one thread is really WAITING , the monitor is unlocked (so than you can have more than one thread waiting on same object). So, if "more than one thread waiting to obtain the monitor", we cannot suppose that they are all wait(), but actually holding to get the monitor (repetitive: logically).
The answer had to be "When there is more than one thread waiting ON THE SAME object, there is no way to be sure which thread will be notified by the notify() method."
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Wed Feb 25, 2015 11:33 am
by admin
I am sorry I am not sure I understand what you mean. "waiting to obtain the monitor of an object" means the same as "waiting on the same object".
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Wed Feb 25, 2015 11:50 am
by ThufirHawat
it means is waiting to get the lock, on the synchronized.
synchronized (object) {...}
So is different from waiting on object.wait()
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Wed Feb 25, 2015 9:15 pm
by admin
Both are same. Why do you think they are different?
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Wed Feb 25, 2015 10:30 pm
by ThufirHawat
Yes, there is just one monitor, is the same.
I thought that was a tricky question, because of the fact it can be the synchronized(object) wait. "waiting to obtain the monitor of an object" can be that. And if so, is very different. But OK, maybe I figured very literary. NP
Tnx for your attention.
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Mon Jun 20, 2016 2:29 pm
by dieterdde
ThufirHawat wrote:Yes, there is just one monitor, is the same.
I thought that was a tricky question, because of the fact it can be the synchronized(object) wait. "waiting to obtain the monitor of an object" can be that. And if so, is very different. But OK, maybe I figured very literary. NP
Tnx for your attention.
Hi, exact same sentiment for me, I chose the right answer as the other ones were not correct, but I don't fully understand why the answer is correct, in terms of wording again:
For me, when you are "waiting to obtain the monitor on an object", it can mean 2 things depending how literal the word "waiting" is in this sentence:
1) It could mean: threads that called obj.wait() (from a synchronised context of course). These are in Thread.State WAITING
This is what's implied, and that's why this answer is the correct one. Since, if you have multiple that called wait(), and another thread calls notify() only 1 of the threads will get that signal.
2) "waiting to obtain the monitor on an object" is the EXACT definition in the javadoc for the Thread.State BLOCKED: A thread that is blocked waiting for a monitor lock is in this state.
When your thread is blocked waiting on a lock, it doesn't mean that your thread called wait() before, it can simply mean that your thread called a method that is synchronized on the object, and you are blocked for further progress until you've obtained the lock on said object.
There is no wait() call involved in this scenario, hence notify() is irrelevant, right?
Please confirm how I need to deal with such questions in the exam when they talk about "waiting" does it mean wait() was called or does it mean Thread.State WAITING or can it also mean Thread.State BLOCKED?
Please advise.
Cheers
Re: About Question enthuware.ocpjp.v7.2.1338
Posted: Mon Jun 20, 2016 8:41 pm
by admin
You are right. This is ambiguous.
Unfortunately, Thread.WAIT says: Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
and Thread.BLOCKED says: Thread state for a thread blocked waiting for a monitor lock.
It is best to stick with what the official JavaDoc says. So we have updated the option to make it clear that it is talking about threads that have called the wait method.
thank you for your feedback!
Paul.