doubt on notification of Threads
Forum » SCJP2 Discussion / threads » doubt on notification of Threads
Started by: itsraghzitsraghz
On: 1200818934|%e %b %Y, %H:%M %Z|agohover
Number of posts: 2
rss icon RSS: New posts
Summary:
This is not a question of an official exam. It is a question extracted from the certification book named "SCJP Sun Certified Programmer for Java 5 Study Guide", and it appears on the page 750. (from JavaRanch Post)
doubt on notification of Threads
itsraghzitsraghz 1200818934|%e %b %Y, %H:%M %Z|agohover

I thought that when a thread calls wait() over an object, and then someother thread calls notify(), the first one leaved the "waiting" state, and goes to the "locked" state, waiting for the second one to leave the synchronized region, and finally goes to "runnable".

But just when i thought that it was fairly clear, I found a question like this one:

Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
 
A. After object B is notified, or after two seconds.
B. After the lock on B is released, or after two seconds.
C. Two seconds after object B is notified.
D. Two seconds after lock B is released.
 
Which one is the correct?

The books says that A, but I simply don't get it, to me, there are no correct question.

unfold doubt on notification of Threads by itsraghzitsraghz, 1200818934|%e %b %Y, %H:%M %Z|agohover
Re: doubt on notification of Threads
itsraghzitsraghz 1200819114|%e %b %Y, %H:%M %Z|agohover

Reply from yogi maheshnath:

Beacause its specified in syntax of wait() method in java.lang.Object.

public final void wait(long timeout)throws InterruptedException
when wait() is called, the thread becomes disabled for scheduling and lies dormant until one of four things occur:

1. another thread invokes the notify() method for this object and the scheduler arbitrarily chooses to run the thread
2. another thread invokes the notifyAll() method for this object
3. another thread interrupts this thread
4. the specified wait() time elapses //see this one

So,After calling wait() method, thread A will become a candidate to get another turn at the CPU,after object B is notified, or after two seconds(wait(2000)) in your example.

Reply from Henry Wong, a bartender of JavaRanch:

B. After the lock on B is released, or after two seconds.

Wrong, but debateable. You can release the lock, and not send the notification, and the thread will not wake up. However, you can argue that two seconds later it will wake up, which technically is still *after* the lock is released.

C. Two seconds after object B is notified.

Clearly wrong. If you sleep for 2 seconds, then send the notification, the thread will wakeup immediately (assuming that it can get the lock).

D. Two seconds after lock B is released.

Clearly wrong. If you sleep for 2 seconds, then release the lock, the thread will wakeup immediately — and not two seconds after. (assuming that it can get the lock).

A. After object B is notified, or after two seconds.

Mostly right. It will wakeup sometime *after* getting the notification. Or it will wakeup sometime *after* 2 seconds. The time to acquire the lock doesn't change that — it will still be after. Of course, you can argue that the *after* implies *immediately after*…

Which one is the correct?

Now, you have 2 answers that are clearly wrong. One that is wrong, but you can argue that it is right, because one part covers the other. And one that is right, but you can argue that it is wrong, because you imply that the word after assumes immediately.

Given this in a test, which one will you pick?

unfold Re: doubt on notification of Threads by itsraghzitsraghz, 1200819114|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.