This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ShankarS
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Akanimo Udoh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar
i would be able to give some kind of help if you could give me description of why you'd really want to pause for a while before allowing the thread to kick in? Are you sure this is the best way to achieve your end? The truth is that this approach might not really be the best approach to solve your 'actual' problem. For example was there any reason for not using the Thread.sleep(...) method instead?
Akanimo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic