posts - 4, comments - 10, trackbacks - 0, articles - 2

package com.time;

import java.io.IOException;
import java.util.*;

public class TimerTaskDemo {
 
    private Timer timer;
 
    public TimerTaskDemo(){}
 
    public TimerTaskDemo(Timer timer){
        this.timer = timer;
        timer.schedule(new MyTask(),3000);
    }

 public static void main(String[] args) throws IOException{
        Timer timer = new Timer();
        TimerTaskDemo timerDemo = new TimerTaskDemo(timer);  
 }
  class MyTask extends TimerTask  {//TimerTask 实现了Runable接口
        public void run() {
               // TODO 自动生成方法存根
               System.out.println("Task is executed!");
                timer.cancel();
        }
    }
}

三秒钟后输出Task is executed!


只有注册用户登录后才能发表评论。


网站导航: