Three ways to schedule Java tasks

Source: Internet
Author: User

/**
* Normal Thread
* This is the most common, create a thread, and then let it run in the while loop,
* Through the sleep method to achieve the effect of timed tasks. This can be done quickly and easily, the code is as follows
*/
public class Task1 {
public static void Main (string[] args) {
Run in a second
Final long timeinterval = 1000;
Runnable Runnable = new Runnable () {
public void Run () {
while (true) {
-------code for task to run
SYSTEM.OUT.PRINTLN ("Hello!!");
-------ends here
try {
Thread.Sleep (timeinterval);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
};
Thread thread = new Thread (runnable);
Thread.Start ();
}
}


Import Java.util.Timer;
Import Java.util.TimerTask;

/**
*
* In the first way compared to the advantage 1> when starting and canceling tasks you can control 2> the first time you perform a task, you can specify the delay time you want
*
* When implemented, the timer class can dispatch tasks, timertask by implementing specific tasks in the run () method. A timer instance can dispatch multiple tasks, which are thread-safe.
* When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here is the code
*/
public class Task2 {
public static void Main (string[] args) {
TimerTask task = new TimerTask () {
@Override
public void Run () {
Task to run goes here
SYSTEM.OUT.PRINTLN ("Hello!!!");
}
};
Timer timer = new timer ();
Long delay = 0;
Long Intevalperiod = 1 * 1000;
Schedules the task to being run in an interval
Timer.scheduleatfixedrate (task, delay, intevalperiod);
}//End of Main
}

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

/**
* Scheduledexecutorservice is introduced as a concurrency tool class from the Java SE5 java.util.concurrent, which is the ideal way to implement timed tasks.
* Compared to the last two methods, it has the following benefits:
* 1> is a thread pool way to perform tasks compared to a single thread of a timer
* 2> can be flexible to set the first time to execute a task delay
* 3> provides a good convention to set the time interval for execution
*
* Below is the implementation code, we show this example through Scheduledexecutorservice#scheduleatfixedrate, through the control of the parameters in the code, the first execution added delay time.
*/
public class Task3 {
public static void Main (string[] args) {
Runnable Runnable = new Runnable () {
public void Run () {
Task to run goes here
SYSTEM.OUT.PRINTLN ("Hello!!");
}
};
Scheduledexecutorservice Service = Executors
. Newsinglethreadscheduledexecutor ();
The second parameter is the delay time for the first execution, and the third parameter is the time interval for the scheduled execution
Service.scheduleatfixedrate (runnable, 1, timeunit.seconds);
}
}

Three ways to schedule Java tasks

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.