java TimerTask增加时间?
您好,我使用以下计时器任务,我想在发生某种情况时增加此任务的时间,
Timer timer2=new Timer();
timer2.schedule(new TimerTask(){
public void run(){
//whatevr
}
}, 4000);
例如
if(mycondition)
{
increase time????
}
我该怎么做
Hi m using the following timer task,and i want to increase the time of this task when a certain condition occurs
Timer timer2=new Timer();
timer2.schedule(new TimerTask(){
public void run(){
//whatevr
}
}, 4000);
examlpe
if(mycondition)
{
increase time????
}
how can i do that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
TimerTask
提取到内部类或独立类中。取消当前正在运行的计时器任务并安排一个增加时间段的新实例。Extract the
TimerTask
in an inner or standalone class. Cancel currently running timer task and schedule a new instance with increased time period.你不能。您必须在增加的时间段内安排一项新任务。如果上一个任务已过时,请确保您
cancel()
它。为了便于将来参考,我建议您使用
Executors
框架。You can't. You'll have to schedule a new task with the incremented period. And if the previous task has become obsolete, make sure that you
cancel()
it.For future reference, I recommend you utilize the
Executors
framework.如有必要,从 run() 提交另一项任务:
Submit another one task from run() if necessary: