public static void main(String[] args) {
final Timer timer = new Timer();
TimerTask task = new TimerTask(){
int count = 0;
int wj = 10;
@Override
public void run() {
count++;
if (count % 4 == 0) {
wj++;
System.out.println("放入玩具1个,现总数="+wj);
}
if (count % 6 ==0) {
wj-=3;
System.out.println("取出玩具3个,现总数="+wj);
}
if (wj < 3) {
System.out.println("玩具现总数="+wj+",不足三个,程序结束");
timer.cancel();
}
}
};
/**
* task--这是被调度的任务。
* delay--这是以毫秒为单位的延迟之前的任务执行。
* period--这是在连续执行任务之间的毫秒的时间。
*/
timer.scheduleAtFixedRate(task, 0, 20000);
}