我正在开发一个程序,该程序将从数据源读取数据,并在读取数据时将其发布。我有一个读取器和一个写入器,读取器产生了几个线程来读取它需要读取的所有数据,将数据放入队列中,然后写入器从队列中读取数据并发布。
我有一个读者专用的控制器和一个作家的控制器。控制器实现了该Callable
接口,但是可以实现该Runnable
接口,因为我的调用返回是Void
。
我想使用执行程序来运行两个控制器。阅读器控制器将需要每X分钟调用一次(并且X大于控制器运行所花费的时间)。
现在,我正在创建的列表Callables
,将其发送到ExecutorService,即:
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分钟后)运行可调用对象?
好吧,你可以用几种方法做到这一点。但是,如果性能很重要,则可以在自己的线程中处理这些事情,如下所示:
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;
}
}
}
}
而且您也可以像这样使用它:
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");
}
});
希望我能为您服务!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句