Alternatives to Thread.sleep()

Hi Guys,

Is there a different way of letting my class wait for 10 seconds instead of using Thread.sleep(). I can't use Thread.speep() as it keeps getting Interrupted and comming out off sleep. It is a multithreaded application, with threads listining on other threads, so I can't use sleep() method

Thankyou

Sunath Kodali

[348 byte] By [vazraa] at [2007-11-27 10:43:49]
# 1

You need to revist what you are doing here. Like review the following

- what is interrupting your thread and why?

- why are you not waiting?

cotton.ma at 2007-7-28 20:02:59 > top of Java-index,Java Essentials,Java Programming...
# 2

Hmm....

<obnoxious>

void reallyInefficientSleep(long millis) {

long now = new Date().getTime();

long eventually = now + millis;

while(new Date().getTime() < eventually) {}

}

</obnoxious>

jGardnera at 2007-7-28 20:02:59 > top of Java-index,Java Essentials,Java Programming...
# 3

If somebody is interrupting your thread that's a sign you shouldn't be sleeping any more. The real question is why do you think you need to sleep in the first place?

ejpa at 2007-7-28 20:02:59 > top of Java-index,Java Essentials,Java Programming...
# 4

Use a Timer.

camickra at 2007-7-28 20:02:59 > top of Java-index,Java Essentials,Java Programming...
# 5

Use a socket read or accept or connect timeout. Use Object.wait(timeout). Use java.util.concurrent.Semaphore.tryAcquire(permits, timeout, ...).

IOW Thread.sleep() is probably not the right tool for the job, whatever that may be.

ejpa at 2007-7-28 20:02:59 > top of Java-index,Java Essentials,Java Programming...