Java Scheduledexecutorservice Tutorial __java

Source: Internet
Author: User
Tags time interval

Scheduledexecutorservice extends the Executorservice interface, providing the ability to perform tasks regularly (like a timer). API Description

method
Desc
Schedule (callable<v> callable, long delay, timeunit unit) Creates and executes a scheduledfuture that is enabled after a given delay.
Schedule (Runnable command, long delay, timeunit unit) Creates and executes a one-time operation that is enabled after a given delay.
Scheduleatfixedrate (Runnable command, long Initialdelay,long period, timeunitunit) Creates and executes a periodic operation that was first enabled after a given initial delay, with a given period of time; that is, the execution begins after InitialDelay, then executes after Initialdelay+period, followed by InitialDelay + 2 * Period After execution, and so forth.
Schedulewithfixeddelay (Runnable command, Long initialdelay, long delay,timeunit unit) Creates and executes a periodic operation that was first enabled after a given initial delay, followed by a given delay between execution termination and the start of the next execution.


The schedule method is used to delay the specified time to perform a specified task. If you need to perform recurring tasks periodically, you can use either the Scheduleatfixedrate or the Schedulewithfixeddelay method, which is different in that the former is performed at a fixed frequency and the latter at a relatively fixed frequency.

Scheduleatfixedrate and Schedulewithfixeddelay do not cause the same task to be executed concurrently, regardless of whether the task's execution time is greater than the interval.

If any execution the of this task takes longer than its period, then subsequent executions may start late, but would not concur rently Execute. Attention Matters

Periodic tasks performed through Scheduledexecutorservice, if an exception is thrown during the execution of a task, the Scheduledexecutorservice stops performing the task and the subsequent tasks are not executed.

If any execution to the task encounters an exception, subsequent executions are. Simple Example 1. Regular execution

Package Com.bytebeats.codelab;

Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;
Import Java.util.concurrent.TimeUnit;

/**
 * ${description}
 * *
 @author Ricky Fung
 * @create 2016-08-15
23:38/public class Scheduledexecutorservicedemo {public

    static void Main (string[] args) throws Interruptedexception {

        Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
        Scheduledexecutorservice.schedule (New Runnable () {
            @Override public
            void Run () {

                System.out.println ( "Count ...");
            }
        , 1, timeunit.seconds)

        ; TimeUnit.SECONDS.sleep (5);
        Scheduledexecutorservice.shutdown ();
    }

2, fixed frequency to perform the task
Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
        Scheduledexecutorservice.scheduleatfixedrate (New Runnable () {
            @Override public
            void Run () {

                System.out.println ("Count ...");
            }
        , 1, 2, timeunit.seconds);

        TimeUnit.SECONDS.sleep (ten);
        Scheduledexecutorservice.shutdown ();
3, fixed time interval to perform the task
Scheduledexecutorservice Scheduledexecutorservice = Executors.newscheduledthreadpool (3);
        Scheduledexecutorservice.schedulewithfixeddelay (New Runnable () {
            @Override public
            void Run () {

                System.out.println ("Count ...");
            }
        , 1, 1, timeunit.seconds);
reference materials

Https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

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.