How Java handle the Time pause
Hi Everyone,
I want to write a simple program for a class to doing somthing after a specific time pause.
For example, A class print out message after the time pasue 15s, such as following:
-
class aaa extends Thread
public run(){
try{
sleep(15000);
System.out.println("after a time pause");
}catch(Exception e){
}
}
-
But it is having other method to doing a time pause without using thread programming.
Thank You very much for everyone can help
You shouldn't (in general) ever need to extend Thread.
You should call static members either by qualifying them with the class name, or by using import static.
import static java.lang.Thread.sleep;
class Sleeper implements Runnable {
public void run() {
try {
sleep(15000);
System.out.println("after a time pause");
} catch (InterruptedException ex) {
System.err.println("woke up");
}
}
}
>But it is having other method to doing a time pause without using thread programming.
In Java SE, Thread.sleep() is how you pause the execution of a thread, including the main thread of a program. Why do you want to use a different method?