gpt4 book ai didi

Java Timer 在特定时间停止进程

转载 作者:行者123 更新时间:2023-12-01 15:14:50 25 4
gpt4 key购买 nike

如何在特定时间停止java中的计时器?

最佳答案

Timer particularTimer = new Timer();
final Timer certainTimer = new Timer();//u can declare a final timer for certain process
particularTimer.schedule(new TimerTask() {
public void run(){
System.out.println("run particularTimer");
certainTimer.schedule(new TimerTask() {
public void run() {
System.out.println("run certainTimer");
}
}, 5000);// it's my start time in milliseconds

}
}, 5000);

//eg
Thread.sleep(6000);
particularTimer.cancel();//when u cancel the particularTimer,and u can cancel the certainTimer too.
certainTimer.cancel();

关于Java Timer 在特定时间停止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com