A**o 发帖数: 1550 | 1 为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出?
有人给解释一下么? |
t****r 发帖数: 25 | 2 what ur code look like??
【在 A**o 的大作中提到】 : 为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出? : 有人给解释一下么?
|
l****u 发帖数: 2166 | 3 details too little ya
【在 A**o 的大作中提到】 : 为什么明明TimerTask已经结束了,但是那个thread还来在那里不退出? : 有人给解释一下么?
|
A**o 发帖数: 1550 | 4 public static void main(String[] args){
new Timer().schdule(new MyTimerTask(), new Date());
}
为什么程序不能正常推出呢? |
A**o 发帖数: 1550 | 5 如果
public static void main(String[] args){
new Timer(true).schdule(new MyTimerTask(), new Date());
Thread.sleep(2000);
}
就可以了。 |
t****r 发帖数: 25 | 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); : } : 就可以了。
|
A**o 发帖数: 1550 | 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.
|
h*****g 发帖数: 1 | 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 |
A**o 发帖数: 1550 | 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
|
m******t 发帖数: 2416 | 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()); : } : 为什么程序不能正常推出呢?
|
A**o 发帖数: 1550 | 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());
|
m******t 发帖数: 2416 | 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.
|