Java timed Task Interface Scheduledexecutorservice

Source: Internet
Author: User

First, Scheduledexecutorservice design thought

Scheduledexecutorservice is a timed task class based on thread pool design, where each dispatch task is assigned to a thread in the thread pool to execute, that is, the task is executed concurrently and does not affect each other.

It is important to note that Scheduledexecutorservice only actually starts a thread when the dispatch task comes, and the rest of the time Scheduledexecutorservice is out of the state of the polling task.

1. Thread Tasks

class Implements Runnable {        private  String jobName;        Myscheduledexecutor () {            }        myscheduledexecutor (String jobName) {        this. JobName=  jobName;    }    @Override    publicvoid  run () {                + ' is running ');}    }

2. Scheduled Tasks

 Public Static voidMain (string[] args) {Scheduledexecutorservice service= Executors.newscheduledthreadpool (10); LongInitialDelay = 1; LongPeriod = 1; //1 seconds from now, once every 1 seconds job1Service.scheduleatfixedrate (NewMyscheduledexecutor ("Job1"), InitialDelay, period, timeunit.seconds); //2 seconds from now, once every 2 seconds job2Service.schedulewithfixeddelay (NewMyscheduledexecutor ("Job2"), InitialDelay, period, timeunit.seconds); }

Two of the most commonly used scheduling methods in Scheduledexecutorservice are Scheduleatfixedrate and Schedulewithfixeddelay. Scheduleatfixedrate Each execution time is pushed back one time interval for the last task, that is, each execution time is: InitialDelay, Initialdelay+period, Initialdelay+2*period, ... Schedulewithfixeddelay each execution time pushes back one time interval for the last task end, that is, each execution time is: InitialDelay, Initialdelay+executetime+delay, Initialdelay+2*executetime+2*delay. Therefore,Scheduleatfixedrate is based on fixed time interval for task scheduling, Schedulewithfixeddelay depends on the duration of each task execution , is based on the time interval of the task scheduling.

Reference documents:

http://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

Java timed Task Interface Scheduledexecutorservice

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.