gpt4 book ai didi

java - 使用 java.util.Timer 后程序不会终止

转载 作者:行者123 更新时间:2023-12-01 12:31:35 26 4
gpt4 key购买 nike

我有简单的Timer控制台应用程序。为什么这个应用程序在执行 run 过程后没有停止并且仍在等待某些东西?

package timer_old;
import java.util.Timer;
import java.util.TimerTask;



public final class FetchMail extends TimerTask {

public static void main (String[] args)
{
System.out.println("starting");
TimerTask fetchMail = new FetchMail();
Timer timer = new Timer();
timer.schedule(fetchMail, 500);
//timer.cancel();
System.out.println("exiting");
}

public void run()
{
System.out.println("Fetching mail...");
}


}

输出:

starting
exiting
Fetching mail...

最佳答案

来自the docs :

By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.

关于java - 使用 java.util.Timer 后程序不会终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25892316/

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