使用ScheduledExecutorService在Java中定期运行任务 [英] Using a ScheduledExecutorService to run a task on a periodic basis in Java
问题描述
我正在研究一个程序,该程序将从数据源读取数据,并在读取数据时将其发布.我有一个读取器和一个写入器,读取器产生了几个线程来读取它需要读取的所有数据,将数据放入队列中,然后写入器从队列中读取数据并将其发布.
I am working on a program that will read data from a data source, and publish that data as it is read. I have a reader and a writer, the reader spawns several threads to read all the data it needs to read, puts the data into a queue, and the writer reads the data from the queue an publishes it.
我有一个读者专用的控制器和一个作家专用的控制器.控制器实现了 Callable
接口,但是可以实现 Runnable
接口,因为我的回叫是 Void
.
I have a controller for my readers and a controller for my writers. The controllers implement the Callable
interface, but could implement the Runnable
interface as my call return is Void
.
我想使用执行程序来运行两个控制器.阅读器控制器将需要每X分钟调用一次(并且X大于控制器运行所花费的时间).
I want to use an executor to run the two controllers. The reader controller will need to be invoked every X minutes (and X is greater than the time it takes the controller to run).
现在,我正在创建 Callables
的列表,并将它们发送到ExecutorService,即:
Right now, I am creating a list of Callables
, sending them to an ExecutorService that is:
List<Future<Void>> futures = ExecutorService es = new Executors.newFixedThreadPoll(2);
for(Future<Void> future: futures) {
try {
future.get();
} catch (Exception e) {
// log the error
}
}
我如何将其转换为调度执行器,该执行器每30分钟(或更确切地说,在上一次作业运行30分钟后)运行可调用对象?
How can I turn this into a scheduling executor that runs the callables every 30 minutes (or more precisely, 30 minutes after the last job ran)?
推荐答案
好的,您可以通过几种方法来做到这一点.但是如果性能重要,您可以在自己的线程中处理这些事情,如下所示:
Okay you can do it several ways . but if performance is important you could handle these things in you own thread like this :
public class TaskTimer extends Thread {
private java.util.concurrent.LinkedBlockingQueue<Runnable> taskQueue;
private int timeToWait;
private Long lastTime = -1l;
public TaskTimer(int time)
{
if(time<0)
throw new IllegalStateException("time can not negative");
timeToWait = time;
taskQueue = new java.util.concurrent.LinkedBlockingQueue<>();
}
void scheduleTask(Runnable task) throws InterruptedException {
taskQueue.put(task);
}
boolean tryScheduleTask(Runnable task) {
return taskQueue.add(task);
}
@Override
public void run() {
while (true)
{
try {
Runnable a = taskQueue.take();
if(!(lastTime==-1 || System.currentTimeMillis()-lastTime>timeToWait))
{
//so wait !
synchronized (lastTime)
{
lastTime.wait(timeToWait-(System.currentTimeMillis()-lastTime));
}
}
try{
a.run();
lastTime = System.currentTimeMillis();
}catch (Throwable e)
{
//todo handle e
}
} catch (InterruptedException e) {
break;
}
}
}
}
,您也可以像这样使用它:
and also you can use it like this :
TaskTimer t = new TaskTimer(2000);
t.start();
t.scheduleTask(new Runnable() {
@Override
public void run() {
System.out.println("1");
}
});
t.tryScheduleTask(new Runnable() {
@Override
public void run() {
System.out.println("2");
}
});
希望我能帮助您!
这篇关于使用ScheduledExecutorService在Java中定期运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!