Answers:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(yourRunnable, 8, 8, TimeUnit.HOURS);
尝试这种方式->
首先创建一个运行任务的TimeTask类,它看起来像:
public class CustomTask extends TimerTask {
public CustomTask(){
//Constructor
}
public void run() {
try {
// Your task process
} catch (Exception ex) {
System.out.println("error running thread " + ex.getMessage());
}
}
}
然后在主类中实例化任务并在指定日期开始定期运行它:
public void runTask() {
Calendar calendar = Calendar.getInstance();
calendar.set(
Calendar.DAY_OF_WEEK,
Calendar.MONDAY
);
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Timer time = new Timer(); // Instantiate Timer Object
// Start running the task on Monday at 15:40:00, period is set to 8 hours
// if you want to run the task immediately, set the 2nd parameter to 0
time.schedule(new CustomTask(), calendar.getTime(), TimeUnit.HOURS.toMillis(8));
}
AbstractScheduledService
按照以下说明使用Google Guava :
public class ScheduledExecutor extends AbstractScheduledService
{
@Override
protected void runOneIteration() throws Exception
{
System.out.println("Executing....");
}
@Override
protected Scheduler scheduler()
{
return Scheduler.newFixedRateSchedule(0, 3, TimeUnit.SECONDS);
}
@Override
protected void startUp()
{
System.out.println("StartUp Activity....");
}
@Override
protected void shutDown()
{
System.out.println("Shutdown Activity...");
}
public static void main(String[] args) throws InterruptedException
{
ScheduledExecutor se = new ScheduledExecutor();
se.startAsync();
Thread.sleep(15000);
se.stopAsync();
}
}
如果您有更多这样的服务,那么在ServiceManager中注册所有服务将是一件好事,因为可以一起启动和停止所有服务。在此处阅读有关ServiceManager的更多信息。
这两个类可以一起计划一个定期任务:
import java.util.TimerTask;
import java.util.Date;
// Create a class extending TimerTask
public class ScheduledTask extends TimerTask {
Date now;
public void run() {
// Write code here that you want to execute periodically.
now = new Date(); // initialize date
System.out.println("Time is :" + now); // Display current time
}
}
import java.util.Timer;
public class SchedulerMain {
public static void main(String args[]) throws InterruptedException {
Timer time = new Timer(); // Instantiate Timer Object
ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
time.schedule(st, 0, 1000); // Create task repeating every 1 sec
//for demo only.
for (int i = 0; i <= 5; i++) {
System.out.println("Execution in Main Thread...." + i);
Thread.sleep(2000);
if (i == 5) {
System.out.println("Application Terminates");
System.exit(0);
}
}
}
}
参考https://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/
如果您的应用程序已经在使用Spring框架,则您已内置了Scheduling
每秒做某事
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//code
}
}, 0, 1000);
我使用Spring Framework的功能。(spring-context jar或maven依赖项)。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTaskRunner {
@Autowired
@Qualifier("TempFilesCleanerExecution")
private ScheduledTask tempDataCleanerExecution;
@Scheduled(fixedDelay = TempFilesCleanerExecution.INTERVAL_TO_RUN_TMP_CLEAN_MS /* 1000 */)
public void performCleanTempData() {
tempDataCleanerExecution.execute();
}
}
ScheduledTask是我自己的接口,带有自定义方法 execute,我将其称为计划任务。
您是否尝试过使用注释的Spring Scheduler?
@Scheduled(cron = "0 0 0/8 ? * * *")
public void scheduledMethodNoReturnValue(){
//body can be another method call which returns some value.
}
您也可以使用xml进行此操作。
<task:scheduled-tasks>
<task:scheduled ref = "reference" method = "methodName" cron = "<cron expression here> -or- ${<cron expression from property files>}"
<task:scheduled-tasks>
我的servlet将此作为代码包含,如果用户按下accept,如何将其保留在调度程序中
if(bt.equals("accept")) {
ScheduledExecutorService scheduler=Executors.newScheduledThreadPool(1);
String lat=request.getParameter("latlocation");
String lng=request.getParameter("lnglocation");
requestingclass.updatelocation(lat,lng);
}
TimeUnit
既适用于initialDelay
和period
。每隔24小时运行一次,最终会在DST启动时被抛出,但是TimeUnit
ofDAYS
不允许您指定细粒度的initialDelay
。(我认为内部ScheduledExecutorService实现DAYS
无论如何都会转换为纳秒)。