如标题:
比如每间隔2小时输出一句“HELLO——WORD!”
[code="java"]
Timer t = new Timer();
final long ms = System.currentTimeMillis();
t.schedule(new TimerTask(){
public void run() {
System.out.println((System.currentTimeMillis()-ms)/1000+", Hello World!");
}
}, 0, 5*1000);
[/code]
public class MyTimer implements Runnable
{
//2小时转化为2*60*60*1000ms(时间间隔)
private static final long STEP=2*60*60*1000;
public static void main(String[] args)
{
//启动线程
new Thread(new MyTimer()).start();
}
//线程主方法
public void run()
{
//启动线程时间
while(true)
{
try {
Thread.sleep(STEP);
System.out.println(" Hello world! ");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
设定一个轮训线程,隔一段时间就运行一次。。
线程 和 timer 都可以!
比如楼上的:
[quote]Timer t = new Timer();
final long ms = System.currentTimeMillis();
t.schedule(new TimerTask(){
public void run() {
System.out.println((System.currentTimeMillis()-ms)/1000+", Hello World!");
}
}, 0, 5*1000); [/quote]