日期:2014-05-20  浏览次数:20772 次

java timer cancle,如何继续原来的程序
比如说这是主文件,简单示意一下
Java code

main(){
   timer t1 = new timer(10);
   System.out.println("123");
}


这是计时器文件
Java code

class timer{
    public timer(int seconds)//每隔seconds秒运行一次,a是调度算法选择 
    {
        // Create local timer
        localTimer = new Timer();
        
        // Create local timer task and set the timer-parameter
        localTimer.schedule(new Dispacther(p,a), 0 , seconds * 1000); 
    }
    
    class Dispacther extends TimerTask{
        public Dispacther(int p,int a){

        }
 
        public void run(){
           localTimer.cancle();//比如在这里取消了任务,怎么再执行main中的输出123?
        }

    } 
}



比如通过cancle取消了计时器的任务,怎么再执行main中的输出123?
小弟没还没学过线程,所以不懂,求指导,谢谢

------解决方案--------------------
提醒楼主:类名首字母大写。
main函数的System.out.println("123");语句是主线程的,肯定会执行,没学过线程还是先去学线程再考虑用jdk的多线程类库吧
------解决方案--------------------
类名是应该大写!
------解决方案--------------------
Java code
Timer timer=new Timer();
  timer.schedule(new TimerTask(){
      public void run(){
        System.out.println("我是定时器");
        timer.cancel();
      }
  ,1000}
timer.cancel();
System.out.println("123");