About Question enthuware.ocpjp.v7.2.1514 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

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

About Question enthuware.ocpjp.v7.2.1514 :

Post by dieterdde »

Dears,

this is the first time so far that I don't agree with the answer: "will cause a thread that had called wait() on that object to be enabled for running"

For 2 reasons:

1) If you had 3 threads that all were waiting that object, then only 1 of the 3 will be enabled for running, the others will still be waiting and not runnable. Because notify() was used instead of notifyAll()

Hence you couldn't make a general statement saying "a thread that called wait() will become runnable (eligible for running)". Only for 1 out of 3 this statement works, so you cannot generalize it to all of them.

Right?

2) I was under the impression, that the thread receiving the notification, will not be immediately runnable (eg eligible for the JVM to pick it for running) because it first has to still get the lock on the object before it can continue running its code. Hence for me the thread might still be blocked before it can become runnable (eligible for running) eg this example (myObj is the same object shared across the 2 threads):

thread 1:
synchronized(myObj) {
try {
myObj.wait();
} catch(InterruptedException e) { }
System.out.printline("OK got notified and got the lock again on myObj, now I can continue this synchronised code block");
//more code

thread 2:
synchronized(myObj) {
myObj.notify();
try {
Thread.sleep(10000); //sleep atleast 10 seconds. Remember, sleep() holds the locks
} catch(InterruptedException e) { }
} //end of synchronized block on myObj, releasing the lock on myObj

In this situation, is it not so that for atleast 10 seconds, thread 1, even though it got notified, will not be runnable but instead is blocked?

I'm still learning so please don't kill me if i'm completely off base!!! But i want to be sure because intending to take the exam in the near future

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

Re: About Question enthuware.ocpjp.v7.2.1514 :

Post by admin »

1. Yes, that is why the option says "a thread" not "all the threads". The next option says "all the threads" and is incorrect as explained in the comment below that option.

2. Yes, the explanation says the same thing:
Note that notifying does not mean that the notified threads will start running. All it means is that they will become eligible for running. They will still have to compete among themselves to acquire the monitor to be able to actually run.
However, one thread will eventually be able to get the lock and become runnable.
Also, a thread can't go to blocked before checking whether the lock is available or not. i.e. if a thread has been notified, it has to check whether the lock is available. If the lock is available it will proceed and if the lock is not available, the thread will go to blocked state. But to check the lock, it has to be able to get the processor first. i.e. be in Runnable state first (even if for a small moment).
As per https://docs.oracle.com/javase/7/docs/a ... tml#notify()
The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object;
How will it compete if the thread directly goes from blocked to blocked? It has to go to Runnable state to do so.
If you like our products and services, please help us by posting your review here.

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

Re: About Question enthuware.ocpjp.v7.2.1514 :

Post by dieterdde »

Thanks a lot for the quick answers!

The first part makes sense now, it's semantics about what means "a thread", I choose to read it as a general statement about threads (analogy: "a child has to be 16 to get a license" <-- statement about all children). But I see how you could read it as "one thread" as well, for sure. Maybe it's because my native language isn't English, I don't know, but I'll have to watch out for that type of question because I might interpret it wrongly because of wording!

The second part, thanks for clearing that up, so what I understand is, when it gets notified, it becomes runnable, checks if it can get the lock, and if not (like in my example) it will go to blocked state. But the answer is right, it will first anyway become runnable, before it would potentially get blocked.

Cheers!

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

Re: About Question enthuware.ocpjp.v7.2.1514 :

Post by admin »

dieterdde wrote: The first part makes sense now, it's semantics about what means "a thread", I choose to read it as a general statement about threads (analogy: "a child has to be 16 to get a license" <-- statement about all children).
You are not wrong in general. But here, there are two options. One says "a thread" and second says, "all the threads". The presence of both the options signifies that the first one is talking about any one thread, while second is talking about all of them.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1514 :

Post by jagoneye »

Even though you are right I hope this type of options are not there in the actual exam since this brings it down to understanding english.I wouldn't be suprised if he option none of these would seem valid to almost everyone. The correct answer should be specific at least in the exam I hope it will be.

Post Reply

Who is online

Users browsing this forum: No registered users and 165 guests