[英]How do I execute a method at a particular time in java?
有什么方法可以用Java编写程序,以便其主要方法调度(或以10-15分钟为间隔)另一个以特定间隔执行的方法?
You can use Job scheduler for this. 您可以为此使用Job Scheduler。 ie 即
Quartz Job Scheduler . 石英作业调度程序 。
Refer this Quartz API 参考这个Quartz API
Or You can use ScheduledExecutorService Java Interface 或者您可以使用ScheduledExecutorService Java接口
Refer this Documentation 请参阅此文档
I think you are looking for the Time class. 我认为您正在寻找时间课程。
See Timer Class API You can use this class like: 请参见Timer类API。您可以使用此类,例如:
You want to perform a Method every 600 miliseconds. 您想每600毫秒执行一次方法。 You write: 你写:
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//Do your stuff
}
};
Timer t = new Timer(600, taskPerfomer);
t.start;
There are more options. 还有更多选择。 This example will be executed once but it can be executed in an interval. 此示例将执行一次,但可以间隔执行一次。 I hope it helps. 希望对您有所帮助。
Use Scheduled Thread pool executor: Schedual your worker thread to execute at every 10 Seconds scheduledThreadPool.schedule(worker, 10, TimeUnit.SECONDS); 使用计划线程池执行程序:安排您的工作线程每10秒执行一次scheduleThreadPool.schedule(worker,10,TimeUnit.SECONDS);
1) Class WorkerThread .java 1)类WorkerThread .java
public class WorkerThread implements Runnable{
private String command;
public WorkerThread(String s){
this.command=s;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Time = "+new Date());
processCommand();
System.out.println(Thread.currentThread().getName()+" End. Time = "+new Date());
}
private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString(){
return this.command;
}
}
2) Class ScheduledThreadPool .java 2)类ScheduledThreadPool .java
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//schedule to run after sometime
System.out.println("Current Time = "+new Date());
for(int i=0; i<3; i++){
Thread.sleep(1000);
WorkerThread worker = new WorkerThread("do heavy processing");
scheduledThreadPool.schedule(worker, 10, TimeUnit.SECONDS);
}
//add some delay to let some threads spawn by scheduler
Thread.sleep(30000);
scheduledThreadPool.shutdown();
while(!scheduledThreadPool.isTerminated()){
//wait for all tasks to finish
}
System.out.println("Finished all threads");
}
}
If your task is not so big, you can use Thread.sleep()
method(example 10 iteration with 10 minutes delay): 如果您的任务不是很大,则可以使用Thread.sleep()
方法(例如10次迭代(延迟10分钟)):
public static void main(String[] args) throws InterruptedException {
methodOne();
for (int i = 0; i < 10; i++) {
Thread.sleep(600000);
methodTwo();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.