• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Condition Variable await method

 
Bobby Oberoi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bobby Oberoi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic