About Question enthuware.ocpjp.v7.2.1338

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

About Question enthuware.ocpjp.v7.2.1338

Post 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."

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post 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".

ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post by ThufirHawat »

it means is waiting to get the lock, on the synchronized.
synchronized (object) {...}
So is different from waiting on object.wait()

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post by admin »

Both are same. Why do you think they are different?

ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post 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.

dieterdde
Posts: 19
Joined: Wed May 25, 2016 4:33 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post 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

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1338

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests