且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用ScheduledExecutorService重新安排任务?

更新时间:2023-01-12 18:08:16

围绕Callable .call方法或带有try / catch的Runnable.run方法......

Surround the Callable.call method or the Runnable.run method with a try/catch...

例如:

public void run()
{
    try
    {
        // ... code
    }
    catch(final IOException ex)
    {
        // handle it
    }
    catch(final RuntimeException ex)
    {
        // handle it
    }
    catch(final Exception ex)
    {
        // handle it
    }
    catch(final Error ex)
    {
        // handle it
    }
    catch(final Throwable ex)
    {
        // handle it
    }
}

注意除了编译器告诉你的东西之外的其他东西(我的样本中的IOException)不是一个好主意,但有时候,这听起来像其中之一,它可以工作如果你正确处理它,请输出。

Note that catching anything other than what the compiler tells you too (the IOException in my sample) isn't a good idea, but there are some times, and this sounds like one of them, that it can work out if you handle it properly.

请记住像错误这样的东西非常糟糕 - 虚拟机内存不足等等......所以要小心你如何处理它们(这就是为什么我将它们分成自己的处理程序而不仅仅是捕获(最终的Throwable ex)而不是其他任何东西)。

Remember that things like Error are very bad - the VM ran out of memory etc... so be careful how you handle them (which is why I separated them out into their own handlers rather than just doing catch(final Throwable ex) and nothing else).