Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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

 
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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic