zoukankan      html  css  js  c++  java
  • 任务定时调度Timer

    Timer类类似定时闹钟,本身就是一个线程,用来实现调用其他线程。

    通过继承TimerTask类,获得多线程的能力,在run 方法内部执行任务。

    示例:

    public class TestTimer {
        public static void main(String[] args) {
            Timer t1 = new Timer();//定义计时器;
            MyTask task1 = new MyTask();//定义任务;
            t1.schedule(task1,3000);  //3秒后执行;
            //t1.schedule(task1,5000,1000);//5秒以后每隔1秒执行一次!
            //GregorianCalendar calendar1 = new GregorianCalendar(2010,0,5,14,36,57); 
            //t1.schedule(task1,calendar1.getTime()); //指定时间定时执行; 
        }
    }
     
    class MyTask extends TimerTask {//自定义线程类继承TimerTask类;
        public void run() {
            for(int i=0;i<10;i++){
                System.out.println("任务1:"+i);
            }
        }
    }

  • 相关阅读:
    Codeforces 878A
    Codeforces 873B-Balanced Substring
    codeforces 868C
    51nod 1402 最大值(贪心)
    最小正子段和 贪心
    codeforces 819B
    Codeforces 785D
    Codeforces 864E
    863D
    UVA 1380 A Scheduling Problem
  • 原文地址:https://www.cnblogs.com/timeboy/p/9464436.html
Copyright © 2011-2022 走看看