![](/img/trans.png)
[英]java schedule a callable at pseudo 'fixed' rate (bell curve distribution for example)
[英]Scheduling a Callable at a fixed rate
我有一个要以固定速率运行的任务。 但是,每次执行后我还需要任务的结果。 这是我尝试过的:
任务
class ScheduledWork implements Callable<String>
{
public String call()
{
//do the task and return the result as a String
}
}
不,我尝试使用ScheduledExecutorService
来安排它。 事实证明,您不能以固定速率安排Callable
,只有Runnable
可以这样做。
请指教。
除非您不关心Callable
的返回值,否则您可以将其包装在Runnable
并使用它传递给ScheduledExecutorService
。
public static Runnable runnableOf(final Callable<?> callable)
{
return new Runnable()
{
public void run()
{
try
{
callable.call();
}
catch (Exception e)
{
}
}
};
}
然后当你想提交给ScheduledExecutroService
你可以传递你的Callable
:
ses.scheduleAtFixedRate(runnableOf(callabale), initialDelay, delay, unit);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.