About Question enthuware.ocpjp.v7.2.1099 :
Moderator: admin
-
- Posts: 27
- Joined: Sat Aug 31, 2013 5:38 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1099 :
Are you sure that if some other thread calls this thread's interrupt method, the thread goes in BLOCKED state ?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
Yes, if the control remains within the synchronized block after the InterruptedException. For example, in the following code:
The catch is still within synchronized block, so it has to reacquire the lock.
HTH,
Paul
Code: Select all
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) // InterruptedException will be caught here
e.printStackTrace();
}
}
}
HTH,
Paul
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sat May 30, 2015 4:53 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
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?
'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?
-
- Posts: 4
- Joined: Sat May 30, 2015 4:53 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
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'.
-
- Posts: 3
- Joined: Tue Jan 19, 2016 4:55 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
Okay, so here I don't agree.
The answer from question:
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?
The answer from question:
In fact, according to JLS 17.2.1: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.
SoAn 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.
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?
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1099 :
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?
Edit:How come the thread comes out of waiting state if it is interrupted? Can you explain that?
Who is online
Users browsing this forum: No registered users and 58 guests