java學習筆記之定時器

來源:互聯網
上載者:User

標籤:print   timertask   private   sim   util   rate   date   抽象   super   

定時器
 1 package pack01_timer; 2  3 import java.io.File; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.Timer; 8 import java.util.TimerTask; 9 10 class MyTimer extends TimerTask{11     private Timer t;12     13     public MyTimer(Timer t) {14         super();15         this.t = t;16     }17 18     public MyTimer() {19         super();20         // TODO Auto-generated constructor stub21     }22 23     @Override24     public void run() {25 //        File file = new File("D:\\demo");26 //        File[] listFiles = file.listFiles();27 //        for (File file2 : listFiles) {28 //            if(file2.getName().endsWith(".xml")){29 //                System.out.println("自動解析xml");30 //                t.cancel();31 //            }32 //        }33         System.out.println("時間到了,開始解析");34     }35     36 }37 public class DemoTimer {38     public static void main(String[] args) throws ParseException {39         //Timer類40         41         Timer t = new Timer();42         /*43          * 參1:抽象類別,這個抽象類別用來指定時間到了該幹什麼事情44          * 參2:定時的時間:單位是毫秒45          */46 //        t.schedule(new MyTimer(), 3000);47         //參3:每隔一定時間又去做這件事48 //        t.schedule(new MyTimer(t), 3000, 200); 49         50         String str = "2017-12-29 18:47:15";51         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");52         Date date = sdf.parse(str);53         t.schedule(new MyTimer(), date);54         55         //取消定時器56 //        t.cancel();57     }58 }

 

java學習筆記之定時器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.