在特定时间运行Java线程

在特定时间运行Java线程,第1张

在特定时间运行Java线程

您可以让Runnable安排其“下次运行”。

如,

class Task implements Runnable {    private final ScheduledExecutorService service;    public Task(ScheduledExecutorService service){        this.service = service;    }    public void run(){        try{  //do stuff        }finally{ //Prevent this task from stalling due to RuntimeExceptions. long untilNextInvocation = //calculate how many ms to next launch service.schedule(new Task(service),untilNextInvocation,TimeUnit.MILLISECONDS);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5490682.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存