Java 本身自带的定时任务(基于java.util.Timer,java.util.concurrent.ScheduledExecutorService)

1.java定时任务可以借助 java.util.Timer 来实现

     // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time) 

    public static void timer1() {

          Timer timer = new Timer();

          timer.schedule(new TimerTask() {

              public void run() {

                  System.out.println("-------设定要指定任务--------");

               }

          }, 2000);// 设定指定的时间time,此处为2000毫秒

     }

     // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行(应该说的是只执行一次的情况,这里我只是想统计下,没实际执行)

     // schedule(TimerTask task, long delay, long period)

    public static void timer2() {

            Timer timer = new Timer();

            timer.schedule(new TimerTask() {

                  public void run() {

                      System.out.println("-------设定要指定任务--------");

                   }

              }, 1000, 1000);

      }

      // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。(说的应该是循环执行,不指定特定的时间范围)

     // scheduleAtFixedRate(TimerTask task, long delay, long period)

     public static void timer3() {

          Timer timer = new Timer();

          timer.scheduleAtFixedRate(new TimerTask() {

                  public void run() {

                       System.out.println("-------设定要指定任务--------");

                  }

          }, 1000, 2000);

     }

    // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.(指定了特定时间,而不是时间范围,循环执行)

    // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

   public static void timer4() {

             Calendar calendar = Calendar.getInstance();

             calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时

             calendar.set(Calendar.MINUTE, 0);       // 控制分

             calendar.set(Calendar.SECOND, 0);       // 控制秒

             Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00

             Timer timer = new Timer();

             timer.scheduleAtFixedRate(new TimerTask() {

                         public void run() {

                              System.out.println("-------设定要指定任务--------");

                        }

              }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行  

       }

2. Java定时任务可以用线程的等待来实现

     介绍:创建一个thread,然后让它在while循环里一直运行着,通过sleep方法来达到定时任务的效果。

     实现(只能实现Runnable接口创建方式是匿名内部类):

            final long timeInterval = 1000;

 

            Runnable runnable = new Runnable() {

                 public void run() {

                     while (true) {

                              // ------- code for task to run

                             System.out.println("Hello !!"); 

                             // ------- ends here

                             try { 

                                   Thread.sleep(timeInterval);

                              } catch (InterruptedException e) { 

                                   e.printStackTrace(); 

                             }

                      }

                  } 

               }; 

              Thread thread = new Thread(runnable); 

              thread.start();

               

3.Java可以用java.util.concurrent.ScheduledExecutorService 来实现定时任务(线程池的方式)

   介绍:ScheduledExecutorService是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,

              这是最理想的定时任务实现方式。 相比于上两个方法,它有以下好处:

              1>相比于Timer的单线程,它是通过线程池的方式来执行任务的

               2>可以很灵活的去设定第一次执行任务delay时间

               3>提供了良好的约定,以便设定执行的时间间隔

     举例:通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,

                通过代码里参数的控制,首次执行加了delay时间。

                Runnable runnable = new Runnable() {

                      public void run() { 

                            // task to run goes here

                            System.out.println("Hello !!"); 

                       }

                 }; 

               ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

               // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间

               service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);

 

学习来源:https://blog.csdn.net/paladinzh/article/details/88011279

                  https://blog.csdn.net/xinyuan_java/article/details/51602088

                  

posted @ 2020-08-28 12:18  小窝蜗  阅读(677)  评论(0)    收藏  举报