Usage of Timer in Java _ jquery

Source: Internet
Author: User
Timer is a commonly used tool class used to execute tasks based on time plans. This article introduces the usage of timer in java, involving the knowledge of javatimer, if you are interested in this article, you can refer to this article to check whether the file is updated on a regular basis. Timer is used here.

It is easy to use. just create a new timer and write a subclass of timertask.

The Code is as follows:

Package comz. autoupdatefile; import java. util. timer; import java. util. timerTask; public class M {public static void main (String [] args) {// TODO todo. generated by zoer Timer timer = new Timer (); timer. schedule (new MyTask (), 1000,200 0);} class MyTask extends TimerTask {@ Override public void run () {System. out. println ("dddd ");}}

In this way, you can start executing mytask every two seconds after one second.

Of course, the timer function can also construct a thread by itself, and then use sleep in the thread to simulate and stop for a period of time, and then execute an action.

In fact, you can see the source code of timertask immediately. timertask implements the runnable interface. That is to say, it is done through a thread to execute an operation at intervals through timer.

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.