1
我想知道Java的Scheduled Thread Pool Executor是否可行。我希望STPE以固定的延遲計劃多個Runnables,每個Runnable可以從List<MyObject>
獲取MyObject。我寫下面的代碼似乎沒有任何問題。但是我也希望所有這些Runnables在同一時間執行。這是可能的,請問我該怎麼做?如果沒有,是否有另一種方法可以完成我所需要的?另外,我需要線程每1秒執行一次。ScheduledThreadPoolExecutor與多個Runnables同時安排在同一時間,如何?
private List<MyObject> myObjects...
int corePoolSize = 1;
long initialDelay = 1L;//1 second
long delay = 1L;//1 second
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(corePoolSize);
for (int i = 0; i < myObjects.size(); i++) {
stpe.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
//DO SOMETHING
}
}, initialDelay, delay, TimeUnit.SECONDS);
}
你需要一些'cron' –