Thread.sleep() help?

Hello all. I am running into a bit of a problem. I have a class that implements Runnable, with the following run() method:

publicvoid run()

{

try{

println("Waiting five seconds.");

new Thread(this).sleep(5000);

listConnections();

}catch (Exception e){}

}

My problem is, the thread is not sleeping for 5 seconds, it just continuously prints "Waiting five seconds" and calls the listConnections() method. I have used other sleep() calls in the same manner with no problems. Any ideas what I am doing wrong? Thanks,

dub

[966 byte] By [dub_styleea] at [2007-11-27 11:29:16]
# 1

Try using

Thread.sleep(5000);

instead. Your way appears to be making a new thread and telling that to sleep.

Also, because it calls the listConnections() method, it must therefore be looping outside of the method you showed us, either that, or the listConnections() method calls the run() method.

Message was edited by:

RedUnderTheBed

RedUnderTheBeda at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 2

Ok, I tried that way, with the same results. Like I said, I have other instances using the same method of sleep() that I posted, that work with no problems. For example:

if (name.equals("shutdown")) {

status = CON_DISCONNECTED;

log("Shutdown in 5 seconds.");

new Thread(this).sleep(5000);

log("Shutdown == TRUE");

server.shutDown = true;

}

dub_styleea at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 3

then try:

try{ Thread.sleep(5000); } catch(InterruptedException e){}

Yannixa at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 4

Ok, for some reason when I remove the println() call, the process will sleep for 5 seconds, and then continuously call listConnections(). I'm not sure, but it seems like I may be using Threads incorrectly. I will keep plugging away... thanks for any help.

dub_styleea at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 5

i know your problem here's the solution:

public void run()

{

println("Waiting five seconds.");

try{ new Thread(this).sleep(5000); } catch (Exception e) {}

listConnections();

}

Message was edited by:

Yannix

Yannixa at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 6

Ok I ended up figuring it out. The method that creates the Thread was in a loop and it kept creating new Threads until I ran out of memory. So the Threads were behaving properly, but there was just a **** load of them so the program would crash before I ever hit the 5 second wait. Thanks for your input.

dub_styleea at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 7

yannix

you seem to think that calling sleep() on a thread object will put that thread into sleep, which is not the case.

when calling sleep, the thread which is currently executing (the one calling sleep) will be put to sleep. Not the thread to which a reference was used to call sleep. Sleep is also a static method of the Thread class, so calling Thread.sleep() is the same as calling sleep on a Thread instance, in both cases the thread calling sleep will be put to sleep

Vectorizeda at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 8

Thread.sleep is a static method. It puts the current thread of execution to sleep, so invoking it on an instance doesn't have the effect you expect it to

georgemca at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 9

Thanks for the info everyone :) ... basically

Thread.sleep(500);is identical to

new Thread(this).sleep(500);

right? They seem to have the same effect from my experience. Good to know that they are equivalent.

dub_styleea at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 10

> Thanks for the info everyone :) ... basically

>

> Thread.sleep(500);is identical to

> new Thread(this).sleep(500);

>

> right? They seem to have the same effect from my

> experience. Good to know that they are equivalent.

No. Invoking sleep on an instance of Thread is wrong. It may compile, it may even work as intended but that's a coincidence. Thread.sleep will always put the current thread to sleep. Always

georgemca at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...
# 11

Ok, thanks for the clarification George, I will stick to Thread.sleep() from now on :)

dub_styleea at 2007-7-29 16:26:57 > top of Java-index,Java Essentials,New To Java...