aspose file tools*
The moose likes Java in General and the fly likes Condition Variable await method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Condition Variable await method" Watch "Condition Variable await method" New topic
Author

Condition Variable await method

Bobby Oberoi
Greenhorn

Joined: Dec 14, 2009
Posts: 3
Alright.. Hello everyone..

Its a very simple code.. Two threads.. One puts and one takes out from the array.. Both wait on size condition of being full and empty respectively

Size of Array is 2
Now the question is when i try to put 3rd element.. it waits(100 ms) on it as there is no space..
Now Thread2 runs and pulls out a value..I have made it to sleep for 300 ms before signaling..

When Put will acquire the lock again.. The Return value should be false.. but its true in many cases... Can't figure out why ??

JavaDoc of await
await
boolean await(long time,
TimeUnit unit)
throws InterruptedException
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. This method is behaviorally equivalent to:

awaitNanos(unit.toNanos(time)) > 0


Parameters:
time - the maximum time to wait
unit - the time unit of the time argument
Returns:
false if the waiting time detectably elapsed before return from the method, else true



Example:




Thank you very much...(I can show the output if needed)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Bobby Oberoi wrote:
When Put will acquire the lock again.. The Return value should be false.. but its true in many cases... Can't figure out why ??


Hint: when the take() is waiting those 300ms, does it give up the lock?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bobby Oberoi
Greenhorn

Joined: Dec 14, 2009
Posts: 3
Thanks for the response..

Ya when thread goes to sleep it does allow other thread to work on thier stuff but that does not mean it relinquishes its lock

Javadoc:
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

Please let me know if i am wrong here.. (Anyways my real problem is not related to sleeping of thread.. i just mocked that thing up so it would return after await time has elapsed)

My basic question is:
If ThreadA is waiting for 10 seconds..

Second 0: ThreadA:await..releases the lock
Second 1:ThreadB acquires it and starts working on it
Second 10: Time elapsed for ThreadA to wait but can't reaquire the lock as ThreadB is working on it..
Second 20: ThreadB finishes its work, does signalAll and releases the lock
Second 21: ThreadA awakes..gets the lock... now as per Javadoc.. if time elapses it should return false as in this case... but it also returns true sometimes...

Hope my question ain't stupid..
Bobby Oberoi
Greenhorn

Joined: Dec 14, 2009
Posts: 3
This might sound absurd guys.. But i was get varying results on my Windows box and i am getting as expected results on my Linux box..

don't know what to say...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Basically, what I am implying is that it is an implementation detail. The javadoc mentions that the await() method simply returns true, if it recieved a signal. But the await() method can't return as it needs to own the lock too -- since the other thread went to sleep() with it. So... arguably, you can say that the delay in getting the lock, gave it time to recieve the signal.

If this was just implemented with the synchronization mechanism, then I would probably agree. The signal arrived after the method had decided to return due to timeout, and hence, should be false... but it isn't using the java synchronization mechanism. The Lock and Condition classes are implement optimistically -- so recieving the signal, determining timeout, and getting the lock, are all done atomically.

In other words... both explanations are within the bounds as defined by javadoc.

Henry

 
Don't get me started about those stupid light bulbs.
 
subject: Condition Variable await method