除了ScheduledExecutorService,我可以使用哪些其他“计时器”库?

时间:2013-03-21 18:29:57

标签: java timer

我有什么选择呢?

创建许多计划任务是否需要多个线程?我想只使用一个线程来处理所有这些计划任务。

目前,我正在使用它:

    // 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);

我对线程知之甚少,但我相信这会创建多个线程来处理一些任务。

0 个答案:

没有答案