aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question abt wait() thats matters a lot for timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question abt wait() thats matters a lot for timeout " Watch "A question abt wait() thats matters a lot for timeout " New topic
Author

A question abt wait() thats matters a lot for timeout

ShankarS
Greenhorn

Joined: Jul 29, 2001
Posts: 25
Hi,
I tried running the following piece of code,
String str = new String();
synchronized(str) {
long currentTimeMillisBefore = System.currentTimeMillis();
str.wait(3000);
long currentTimeMillisAfter = System.currentTimeMillis();
System.out.prinltn("Wait Time = " +
(currentTimeMillisAfter-currentTimeMillisBefore));
}
The wait(long) method doesn't wait for the exact time specified.
For example, if call the wait method passing 5000 milliseconds, its waits only
for 4988 milliseconds and comes out. Its doesn't wait exactly for 5000
milliseconds.
I checked the documentation, it says
The thread will lie dormant until one of four things happens:
(1)Some other thread invokes the notify method for this object and thread T
happens to be arbitrarily chosen as the thread to be awakened.
(2)Some other thread invokes the notifyAll method for this object.
(3)Some other thread interrupts thread T.
(4)The specified amount of real time has elapsed, more or less. If
timeout is zero, however, then real time is not taken into consideration and
the thread simply waits until notified.
Since there is a little difference in the wait time, I am planning to have
grace time of about 50 milliseconds. So that the thread waits for the
specified time.
I would like to know, whether anyone faced this problem and the solution
specified above is acceptable (having a grace time)?
Thanx in adavnce,
Shankar S
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Shankar,
It seems to me , the current thread is interrupted before its
waiting period is over.
void wait(long timeout)
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

I believe which ever happens first (notification or the timeout)
the current thread responds, there by it can content for object lock again in the "seeking lock pool"
What do you think?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
You should not depend on timeout in wait or sleep or the count returned by System.currentTimeMillis to be precise. It can certainly never be more precise than the operating system time mechanism, which as I recall has a granularitity of 55 milliseconds in most Windows OS.
Hopefully you realize the example code you gave, if all in single method, would not synchronize anything since the String str is a local variable, it would not be visible to any other Thread calling the method.
Bill
------------------
author of:
ShankarS
Greenhorn

Joined: Jul 29, 2001
Posts: 25
Hi,
I accept that, synchronizing the method local variable is meaningless. But, i jus used fo example purpose.
For what reason, the thread comes out of the wait(long) method.
Due to wait timeout or notify() called by other thread ?
In WIN32 API, we can find the reason. I would like to know the same in java.
Thanx,
Shankar S
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
Thats a good question - as far as I know, you can tell if a Thread has been interrupted, but thats all.
Bill
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Your name "ShankarS" does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements.
Thanks!

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question abt wait() thats matters a lot for timeout