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