Java core programming -- thread timer (TimerTask) (3), javatimertask

Source: Internet
Author: User

Java core programming -- thread timer (TimerTask) (3), javatimertask

1. TimerTask;

TimerTask is a special thread that schedules one or more tasks for Timer. It implements the Runable interface and is an abstract class. He does not implement the run () method by himself, and must implement it by subclass. Unlike a thread, a Timer task is started at a scheduled time, so you cannot use the start () method directly. Instead, you need a special Timer class to start it.

2. Timer

Timer is a tool used to schedule tasks executed in background threads. It can schedule tasks to be executed once or multiple times. Each Timer object corresponds to a backend thread for sequential execution of all Timer tasks.

3. Specific applications

Package se. thread; import java. util. date; import java. util. timer; import java. util. timerTask; public class ThreadTimerTest extends TimerTask {@ Override public void run () {System. out. println (new Date ();} public static void main (String [] args) {ThreadTimerTest threadTimerTest = new ThreadTimerTest (); Timer timer = new Timer (); timer. schedule (threadTimerTest, new Date (), 3000 );}}

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.