aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) 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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "A question abt wait() thats matters a lot for timeout" Watch "A question abt wait() thats matters a lot for timeout" New topic

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


Joined: Jul 29, 2001
Posts: 25
I tried running the following piece of code,
String str = new String();
synchronized(str) {
long currentTimeMillisBefore = System.currentTimeMillis();
long currentTimeMillisAfter = System.currentTimeMillis();
System.out.prinltn("Wait Time = " +
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
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

Joined: Jun 11, 2001
Posts: 48
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?
It is sorta covered in the JavaRanch Style Guide.
subject: A question abt wait() thats matters a lot for timeout