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

来源:互联网 发布:linux系统启动执行命令 编辑:程序博客网 时间:2024/05/27 09:44

周期显示一个数字的变化

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

0 0
原创粉丝点击