Redian新闻
>
这个Timer不退出是怎么回事儿?
avatar
这个Timer不退出是怎么回事儿?# Java - 爪哇娇娃
A*o
1
为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出?
有人给解释一下么?
avatar
t*r
2
what ur code look like??

【在 A**o 的大作中提到】
: 为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出?
: 有人给解释一下么?

avatar
l*u
3
details too little ya

【在 A**o 的大作中提到】
: 为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出?
: 有人给解释一下么?

avatar
A*o
4
public static void main(String[] args){
new Timer().schdule(new MyTimerTask(), new Date());
}
为什么程序不能正常推出呢?
avatar
A*o
5
如果
public static void main(String[] args){
new Timer(true).schdule(new MyTimerTask(), new Date());
Thread.sleep(2000);
}
就可以了。
avatar
t*r
6
i guess if it's a non-daemon thread, it does not need to stop right away.

【在 A**o 的大作中提到】
: 如果
: public static void main(String[] args){
: new Timer(true).schdule(new MyTimerTask(), new Date());
: Thread.sleep(2000);
: }
: 就可以了。

avatar
A*o
7
yeah. that's what java api doc says.
i just want to know the reason behind it. :p

【在 t****r 的大作中提到】
: i guess if it's a non-daemon thread, it does not need to stop right away.
avatar
h*g
8
Once a thread is done, u can not call the start method of the thread object
to rerun it.
Timer is usually used to run the task over and over again during some time
intervals, and it can be shared by multiple threads. finishing task with one
thread doesn't mean the timer thread won't be used by other thread. there
is no good way to tell that if there is a live thread will use it in the
future.
The design can help you save the cost and overhead to terminate and restart
a new timer thread if ever u
avatar
A*o
9
搞得很复杂的说。

object
one
restart

【在 h*****g 的大作中提到】
: Once a thread is done, u can not call the start method of the thread object
: to rerun it.
: Timer is usually used to run the task over and over again during some time
: intervals, and it can be shared by multiple threads. finishing task with one
: thread doesn't mean the timer thread won't be used by other thread. there
: is no good way to tell that if there is a live thread will use it in the
: future.
: The design can help you save the cost and overhead to terminate and restart
: a new timer thread if ever u

avatar
m*t
10

Did you try:
new Timer(true).schdule(new MyTimerTask(), new Date());

【在 A**o 的大作中提到】
: public static void main(String[] args){
: new Timer().schdule(new MyTimerTask(), new Date());
: }
: 为什么程序不能正常推出呢?

avatar
A*o
11
yes, then the timer dies as the main thread dies, instantly.
i'd expect timer dies after its completion.
and so did the whole applicaiton shuts down as no timer is pending.
well, anyway, it was a special requirement.

【在 m******t 的大作中提到】
:
: Did you try:
: new Timer(true).schdule(new MyTimerTask(), new Date());

avatar
m*t
12

I would still try making the timer itself a daemon, and spawn a non-daemon
thread in the timer task to do the actual work.

【在 A**o 的大作中提到】
: yes, then the timer dies as the main thread dies, instantly.
: i'd expect timer dies after its completion.
: and so did the whole applicaiton shuts down as no timer is pending.
: well, anyway, it was a special requirement.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。