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

周期显示一个数字的变化

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的值。


版权声明:本文为zzidea原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。