Two ScheduledExecutorService methods,

Source: Internet
Author: User

Two ScheduledExecutorService methods,

During development, other code is often executed in another thread, and implemented using the java scheduled task interface ScheduledExecutorService.

ScheduledExecutorService is a scheduled task class designed based on the thread pool. Each scheduling task is assigned to a thread in the thread pool for execution. That is to say, the task is executed concurrently without affecting each other.

Note: Only when the scheduling task comes, ScheduledExecutorService will actually start a thread, and ScheduledExecutorService will be in the polling task status for the rest of the time.

1. scheduleAtFixedRate Method

Example:

Import java. text. simpleDateFormat; import java. util. date; import java. util. concurrent. executors; import java. util. concurrent. scheduledExecutorService; import java. util. concurrent. timeUnit; public class ScheduleAtFixedRateDemo {public static void main (String [] args) {ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format executorService. scheduleAtFixedRate (new Runnable () {@ Override public void run () {System. out. println ("++ thread:" + df. format (new Date () ;}, 2, 3, TimeUnit. SECONDS); System. out. println ("++ main:" + df. format (new Date ()));}}

Running result:

++ Main: 15:20:52
++ Thread: 15:20:54
++ Thread: 15:20:57
++ Thread: 15:21:00
++ Thread: 15:21:03
++ Thread: 15:21:06

 

It can be seen that after 2 s, the sub-thread starts to execute and runs every 3 s round robin.

 

2. scheduleWithFixedDelay Method

Example:

Import java. text. simpleDateFormat; import java. util. date; import java. util. concurrent. executors; import java. util. concurrent. scheduledExecutorService; import java. util. concurrent. timeUnit;/*** ScheduleWithFixedDelay usage * @ author Administrator **/public class ScheduleWithFixedDelayDemo {public static void main (String [] args) {ScheduledExecutorService executorService = Executors. newScheduledThreadPool (1); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // set the date format executorService. scheduleWithFixedDelay (new Runnable () {@ Override public void run () {System. out. println ("++ thread:" + df. format (new Date () ;}, 2, 3, TimeUnit. SECONDS); System. out. println ("++ main:" + df. format (new Date ()));}}

Running result:

++ Main: 15:24:32
++ Thread: 15:24:34
++ Thread: 15:24:37
++ Thread: 15:24:40
++ Thread: 15:24:43

 

3. Two differences

ScheduleAtFixedRate each execution time is the interval from the start of the previous task to push back, that is, the execution time is initialDelay, initialDelay + period, initialDelay + 2 * period .....

ScheduleWithFixedDelay each execution time is the interval from the end of the last task to push back, that is, the execution time is: initialDelay, initialDelay + executeTime + delay, initialDelay + 2 * executeTime + 2 * delay .....

It can be seen that ScheduleAtFixedRate is based on a fixed interval for task scheduling, ScheduleWithFixedDelay depends on the duration of each task execution, and is based on a non-fixed interval for task scheduling.

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.