import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class UntilTimerTest {
public static void main(String[] args) throws IOException
{
Timer timer=new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run() {
long now=System.nanoTime();
System.out.println(new Date(now));
}
}, 1000,1000);
while(true)
{
int c=System.in.read();
if(c==’c’)
timer.cancel();
}
}
}
本文转自http://www.examda.com/Java/zhuangye/20071024/104358381.html
本文提供了一个 Java Timer 类的应用实例,演示了如何使用 Timer 安排周期性任务,并通过 System.nanoTime 获取当前时间,同时利用 System.in.read() 方法监听键盘输入以取消任务。

被折叠的 条评论
为什么被折叠?



