您的位置:首页 > 编程语言 > Java开发

Java定时器周期执行任务的例子

2015-03-14 14:40 281 查看
周期显示一个数字的变化

public class MainApp {
private static int count=0;
public static void main(String[] args) throws InterruptedException {
Timer t=new Timer();
TimerTask task=new TimerTask() {
public void run() {
System.out.println(count);
}
};
t.schedule(task, 1000,1000);//时间为毫秒,1秒=1000毫秒
while(true)
{
Thread.sleep(300);
count++;
}

}

}

其他几个schedul貌似不行。这个例子中是没秒显示count的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: