Java timed to perform tasks __java

Source: Internet
Author: User

The most common thing in Java is the timer timer in the Java API. In addition, there are also common thread-based and new API Scheduledexecutorservice interfaces that are provided from Java1.5. : Timer Timer Timer Introduction

A timer is primarily used to perform a task on a regular basis. The common methods include

Method Name Method Description
Schedule (timertask task, Date time) Perform a task task at the time specified by
Schedule (timertask task, Date Firsttime, long period) Start a task task at the time specified by Firsttime and then perform a task after every period time
Schedule (timertask task, long delay) To perform a task task after delaying delay time, starting at the program execution time
Schedule (timertask task, long delay, long period) Perform a task task after the execution of the program, delay delay time, and then perform a task after every period time
Scheduleatfixedrate (timertask task, Date Firsttime, long period) Start a task task at the time specified by Firsttime and then perform a task after every period time
Scheduleatfixedrate (timertask task, long delay, long period) Perform a task task after the execution of the program, delay delay time, and then perform a task after every period time
the difference between schedule and scheduleatfixedrate

In the usual methods of a timer, there are two sets of methods that do not explicitly distinguish between schedule (timertask task, Date Firsttime, long Period), and Scheduleatfixedrate ( TimerTask task, Date Firsttime, long period), and the other group is schedule (timertask task, long delay, long period) and scheduleatfixedrate ( TimerTask task, long delay, long period).
See programs First:

public static void Testschedule () {final SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
  Date D1 = null;
    try {d1 = sdf.parse ("2016/2/19 11:05:00");
    Timer T = new timer (); T.schedule (New TimerTask () {@Override public void run () {System.out.println ("schedule" + SD
      F.format (New Date (System.currenttimemillis ()));
  }, D1, 5 * 1000);
  catch (ParseException e) {e.printstacktrace (); } public static void Testschedulefixrate () {final SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss")
  ;
  Date D1 = null;
    try {d1 = sdf.parse ("2016/2/19 11:05:00");
    Timer T = new timer ();
      T.scheduleatfixedrate (New TimerTask () {int i = 1; @Override public void Run () {System.out.println (i + ": schedulefixrate") + Sdf.format (new Date (Syst
        Em.currenttimemillis ()));
      i++;
  }, D1, 5 * 1000); catch (ParseException e) {E.printstacktrace(); }
}

The results were found to be the same if the current time was earlier than 2016/2/19 11:05:00, and when the current time is later than 2016/2/19 11:05:00, The Scheduleatfixedrate method supplements the number of executions that are missing from the Firsttime to the current time, and then executes at the period interval, and the schedule method executes the first time at the current time, followed by the period time interval. the delay based on multithreading

public static void Threaddelay () {
  final simpledateformat sdf = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
  Thread thread = new Thread (new Runnable () {
    int i = 0;
    @Override public
    Void Run () {while
      (true) {
        System.out.println (i + ": threaddelay") + Sdf.format (new Date (System.currenttimemillis ()));
        i++;
        try {
          thread.sleep (1000);
        } catch (Interruptedexception e) {
          e.printstacktrace ();
        }
    }}
  });

  Thread.Start ();
}
Scheduledexecutorservice Interface

The Scheduledexecutorservice interface is especially useful for execution of concurrent programs.

public static void Scheduledexecutor () {final SimpleDateFormat SDF = new SimpleDateFormat ("yyy
  Y/mm/dd HH:mm:ss ");

  Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (1);
    Scheduledexecutorservice.scheduleatfixedrate (New Runnable () {int i = 0;
      @Override public void Run () {i++;
    SYSTEM.OUT.PRINTLN (i + ": Scheduledexecutor time is" + Sdf.format (new Date ());
  }, 0, 1, timeunit.seconds); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.