我正在使用 ScheduledThreadPoolExecutor 并且我不知道如何处理某些事情。
我正在以这种方式安排一些任务:
scheduledExecService = new ExtendedScheduledExecutor(numThreads, myThreadFactory);
TareaActualizacion act = new TareaActualizacion(inst);
ScheduledFuture<?> handle = scheduledExecService.scheduleWithFixedDelay(act, retrasoInicial, segundosRefresco, TimeUnit.SECONDS);
act 是一个 Runnable 类,它通过参数接收一些数据:
public class TareaActualizacion implements Runnable {
private Instalacion instalacion;
public TareaActualizacion(Instalacion instalacion) {
this.instalacion = instalacion;
}
@Override
public void run() {
//Do something
}
public Instalacion getInstalacion() {
return instalacion;
}
}
现在在 ExtendedSecheduledExecutor 的 afterExecute 方法中,我想获取任务 TareaActualizacion 的对象 Instalacion 但我不知道该怎么做。我的 ExtendedScheduledExecutor 类如下所示:
public class ExtendedScheduledExecutor extends ScheduledThreadPoolExecutor{
public ExtendedScheduledExecutor(int arg0) {
super(arg0);
}
public ExtendedScheduledExecutor(int arg0, ThreadFactory arg1) {
super(arg0, arg1);
}
@Override
protected void afterExecute(Runnable r, Throwable t)
{
super.afterExecute(r, t);
System.out.println("Executing afterExecute. Throwable is " + t);
if (t != null)
t.printStackTrace();
//I need to get the Instalacion attribute from TareaActualizacion task. How can I do it??
}
}
知道如何解决吗?
谢谢!
纽斯