About Question enthuware.ocpjp.v7.2.1099 :

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

Moderator: admin

Post Reply
sinapse
Posts: 27
Joined: Sat Aug 31, 2013 5:38 pm
Contact:

About Question enthuware.ocpjp.v7.2.1099 :

Post by sinapse »

Are you sure that if some other thread calls this thread's interrupt method, the thread goes in BLOCKED state ?

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

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

Post by admin »

Yes, if the control remains within the synchronized block after the InterruptedException. For example, in the following code:

Code: Select all

public void run() {
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (Exception e) // InterruptedException will be caught here
                  e.printStackTrace();
                }
            }

}
The catch is still within synchronized block, so it has to reacquire the lock.

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

rvdberg
Posts: 4
Joined: Sat May 30, 2015 4:53 am
Contact:

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

Post by rvdberg »

about the option
'An exception might be raised if it is not called in a synchronized block otherwise no exception will be raised.'
-It will ALWAYS raise IllegalMonitorStateException if not called in a synchronized block for that object.

I don't get it by taking question enthuware.ocpjp.v7.2.1225 into consideration.
Question: What happens when a method calls wait() without ensuring that the current thread owns the monitor of the object?
Correct answer:An IllegalMonitorStateException will be thrown if current thread does not have the monitor of the object .
While the explanation from an incorrect answer 'An IllegalMonitorStateException will be thrown whenever the method is called.' explicitely states:
"It may not throw the exception if it owns the monitor. (Note that it says 'without ensuring' )".

That sounds like whenever you don't call it from a synchronized context it might have the monitor and run correctly, or it doesn't have the monitor and throws an exception.
This questions implies that it will always throw the exception.

Am i misinterpreting something here?

rvdberg
Posts: 4
Joined: Sat May 30, 2015 4:53 am
Contact:

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

Post by rvdberg »

Nevermind, i think i misunderstood the explanation of that question. I thought 'without ensuring' meant not putting it in synchronized context, however, now I think it meant 'not checking if it is in synchronized context'.

StShadow
Posts: 3
Joined: Tue Jan 19, 2016 4:55 pm
Contact:

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

Post by StShadow »

Okay, so here I don't agree.

The answer from question:
The thread that has called the wait() will not come out of WAITING state until some other thread calls notify or notifyAll on the same object or interrupts the thread.
In fact, according to JLS 17.2.1:
An internal action by the implementation. Implementations are permitted, although not encouraged, to perform "spurious wake-ups", that is, to remove threads from wait sets and thus enable resumption without explicit instructions to do so.
So
The thread that has called the wait() might come out of WAITING state even if no other thread calls notify or notifyAll on the same object or interrupts the thread.

By the way, that's why highly recommended to call wait within loop on some logical condition to avoid spurious wake-ups, if not needed.

Or am I missing something?

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

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

Post by ThufirHawat »

I think the answer is wrong because the other thread must call notify() or notifyAll() and also LEAVE the synchronized block to other thread who had waiting can run again

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

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

Post by admin »

No, the given answer is correct. The waiting thread(s) comes out the the WAIT state as soon as another thread calls the notify(notifyAll) method. If, however, the lock is not available (i.e. until the thread that has called notify doesn't come out of the synchronized block), the thread(s) that was waiting enters the BLOCKED state. It is not "waiting" anymore. Option 1 is talking only about the WAITING state not the RUNNABLE state.

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.1099 :

Post by jagoneye »

I don't know but I am not getting any notifications or a message if I reply or post in any thread related to OCP 7. That aside, after so long I finally see a StackOverFlow link!Thumbs up for that and this is one of the many annoying questions where I got it wrong because of english! Didn't see the might be raised else I was clearly gonna select the first option! Tricky question for me.
Edit:How come the thread comes out of waiting state if it is interrupted? Can you explain that?

Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests