关于timer的问题 求助。
各位大牛马上要交作业了 紧急救助一个问题。  这是两个class,一个负责执行,每次按1后 定时器都是开一次,按固定时间输出abcde。我现在的问题是,每次按完1后,都会出现一个新的定时器,越按越多,abcde的输出越来越快。如何才能做到启动新定时器的时候关闭掉上一个旧的定时器,让输出的abcde永远保持在5秒一次呢?
import java.util.TimerTask;
public class mytask extends TimerTask{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("abcde");		
	}
}
import java.util.Scanner;
import java.util.Timer;
public class test extends Timer{
	public static void main(String[] arg){
		Timer timer = new Timer();
		while(true){
			Scanner sc = new Scanner(System.in);
			int choose = sc.nextInt();
			if(choose == 1){
				timer.schedule(new mytask(),0000,5000);
			}
		}		
	}
}
------解决方案--------------------
一种是取消timer,一种是功能启动后,让输入不造成影响。
取消timer
Java code
public static void main(String[] arg) {
        Timer timer = null;
        while (true) {
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            if (choose == 1) {
                if (timer != null) {
                    timer.cancel();
                }
                timer = new Timer();
                timer.schedule(new mytask(), 0000, 2000);
            }
        }
    }