Timed Task runnable

Source: Internet
Author: User


Description

Recently do CMS want to modify the template locally, and then put the latest template content into the data, so think of runnable write a timed task;

Ideas:

See if the template changes every other minute, and if there is any change, the execution is saved to the database;


Timed Task Core code:

Package com.aih.common.utils;

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

public class Timetask {
public void Dotemplate () {
Runnable runnable=new Runnable () {
@Override
public void Run () {
System.out.println ("Start");
}

};
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);
}

Test
public static void Main (string[] args) {
Timetask timetask=new timetask ();
Timetask.dotemplate ();
}
}



Knowledge Expansion:

  1. /**

  2. * Normal Thread

  3. * This is the most common, create a thread, and then let it run in the while loop,

  4. * Through the sleep method to achieve the effect of timed tasks. This can be implemented quickly and easily, with the following code:

  5. * @author GT

  6.  *

  7.  */

  8. Public class Task1 {

  9. Public Static void Main (string[] args) {

  10. //Run in a second

  11. Final long timeinterval = +;

  12. Runnable Runnable = new Runnable () {

  13. Public void Run () {

  14. while (true) {

  15. //-------code for task to run

  16. System.out.println ("Hello!!");

  17. //-------ends here

  18. Try {

  19. Thread.Sleep (timeinterval);

  20. } catch (Interruptedexception e) {

  21. E.printstacktrace ();

  22. }

  23. }

  24. }

  25. };

  26. Thread thread = new thread (runnable);

  27. Thread.Start ();

  28. }

  29. }


  1. import Java.util.Timer;

  2. import Java.util.TimerTask;

  3. /**

  4.  * 

  5. * In the first way compared to the advantage of 1> when you start and go to cancel a task can control 2> the first time you perform a task can specify the delay time you want

  6.  * 

  7. * 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.

  8. * When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here's the code:

  9.  * 

  10. * @author GT

  11.  * 

  12.  */

  13. Public class Task2 {

  14. Public Static void Main (string[] args) {

  15. TimerTask task = new timertask () {

  16. @Override

  17. Public void Run () {

  18. //Task to run goes here

  19. System.out.println ("Hello!!!");

  20. }

  21. };

  22. Timer timer = New timer ();

  23. long delay = 0;

  24. long intevalperiod = 1 * ;

  25. //Schedules the task to being run in an interval

  26. Timer.scheduleatfixedrate (task, delay, intevalperiod);

  27. } //end of main

  28. }


Timed Task runnable

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.