Task scheduling (iv)--scheduledexecutorservice replacement timer for multi-threaded task scheduling

Source: Internet
Author: User

In the previous blog, "task scheduling (iii)--timer's alternative Scheduledexecutorservice introduction" has made a brief introduction to Scheduledexecutorservice, In fact, the use of Scheduledexecutorservice to replace the timer is also a compelling thing. The main reasons are as follows:

    1. The timer does not support multi-threading, all tasks that are suspended under the timer are single-threaded, the task can only be executed serially, and if one of the tasks takes too long to execute, it can affect the execution of other tasks, and then there may be various incoming problems.
    2. The thread of the timer does not catch an exception, and if an exception is thrown, the only process of the timer is timertask, so that all tasks that are suspended under the timer will not continue to execute.

The first problem, with the explosion of business data, we have several tasks in production now takes 1-3 hours each time, during which time the other tasks under the timer can only wait, which is unbearable. Re-open a timer? Is it not possible to simply open a timer for all the time-consuming tasks, which is too messy.


The second problem is extremely deadly. A lot of business data are timed tasks run out at night, resulting in a thread being killed due to program problems or insufficient memory resources. All the tasks under the timer were not executed, and the next day was a full day, and the main task was to run the task and adjust the data. It's a disaster!


In order to compensate for the shortcomings of the timer, jdk1.5 introduced and contracted, which provides the scheduledexecutorservice. The concrete implementation class is: Scheduledthreadpoolexecutor. Scheduledthreadpoolexecutor supports multiple threads while capturing exceptions in the thread. So it's the perfect replacement for the timer.


Share an example:

/** *  Task2 * @author Arron * @date August 5, 2015 PM 2:08:34  * @version 1.0  */public class Task2 extends timertask{ @SuppressWarnings ("deprecation") @Overridepublic void Run () {System.out.println ("----task2 start--------" +new Date () . toLocaleString ()); try {thread.sleep ();} catch (Interruptedexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("----5s later, Task2 end--------" +new Date (). toLocaleString ());}}
Test code:
public static void Main (string[] args) {Scheduledexecutorservice pool = Executors.newscheduledthreadpool (2);// Enable 2 threads Task1 T1 = new Task1 (),//execute immediately, task consumes 3 seconds, wait 2 seconds after execution, "when there is a free thread", perform the task again Pool.schedulewithfixeddelay (T1, 0, 2, Timeunit.seconds);//execute immediately, task consumes 5 seconds, wait 2 seconds after execution, "when there is a free thread", perform the task again Task2 t2 = new Task2 ();p ool.schedulewithfixeddelay (T2, 0, 2, timeunit.seconds);}
Execution results



Such tasks do not interfere with each other, and can be executed at the same time. But the number of threads to be set. The transition to increasing the number of threads can also backfire.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Task scheduling (iv)--scheduledexecutorservice replacement timer for multi-threaded 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.