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:
/**
* 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 implemented quickly and easily, with the following code:
* @author GT
*
*/
Public class Task1 {
Public Static void Main (string[] args) {
//Run in a second
Final long timeinterval = +;
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 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
*
* 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's the code:
*
* @author GT
*
*/
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 * ;
//Schedules the task to being run in an interval
Timer.scheduleatfixedrate (task, delay, intevalperiod);
} //end of main
}
Timed Task runnable