我有什么选择来做这件事?
创建多个计划任务是否需要多个线程?我只想使用一个线程来处理所有这些计划任务。
目前,我正在使用这个:
// send first message, first message is right away
sendMessage();
// prepare the timer to send second message, 2nd message sends 35 seconds after 1st
final ScheduledFuture<?> future2 = ses.scheduleAtFixedRate(
sendMessage(),
initDelay,
EXE_LENGTH * EXE_LENGTH,
TimeUnit.SECONDS
);
// kill the process for future2 30 seconds after 2nd message is sent
ses.schedule(new Runnable()
{
@Override
public void run()
{
future2.cancel(true);
}
}, initDelay, TimeUnit.SECONDS);
// prepare the timer to send the third message, 3rd message sends 35 seconds after task for 2nd message is killed
final ScheduledFuture<?> future3 = ses.scheduleAtFixedRate(
sendMessage(),
initDelay + 2L,
EXE_LENGTH * EXE_LENGTH,
TimeUnit.SECONDS
);
// kill the process for future3 30 seconds after 3rd message is sent
ses.schedule(new Runnable()
{
@Override
public void run()
{
future3.cancel(true);
}
}, initDelay, TimeUnit.SECONDS);
// shutdown the whole scheduler after the 3rd process is killed
ses.schedule(new Runnable()
{
@Override
public void run() {
shutdown(challengeId);
}
}, initDelay+2L, TimeUnit.SECONDS);
我对线程不太了解,但我相信这会创建多个线程来处理一些任务。