gpt4 book ai didi

java定时器定时任务

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:10 26 4
gpt4 key购买 nike

在下面的情况下会发生什么?

Timer t = new Timer();
t.schedule(...);
t = new Timer();

具体来说,在我将一个新的 Timer 实例分配给 t 之后,我在 Timer t 上安排的任务会发生什么情况?

最佳答案

它们不会消失。每个 Timer 对象都与一个后台进程相关联。即使您删除了程序中对 Timer 的所有引用,后台进程仍将继续运行(它拥有自己对该对象的引用)。因此,该对象不会受到垃圾回收。

参见 official documentation了解详情。

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially ... After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur.

关于java定时器定时任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6666517/

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