Redian新闻
>
急问:怎么kill一个thread, thread.interrupte()不好用呀?
avatar
急问:怎么kill一个thread, thread.interrupte()不好用呀?# Java - 爪哇娇娃
b*n
1
我在MainThread里同时start了两个thread: thread1, thread2
MainThread里等了2sec之后,想要kill thread2
thread2是去obtain一个url 的content,
一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
想要实现在2sec之后,
如果thread2没结束就强行结束,并返回nothing,
如果thread2结束了就返回url content.
用了:thread2.interrupt() 来强行结束thread2;
但是好像没有用呀,thread2还是在运行,并没有end
而且在我连续调用MainThread时,下一次的MainThread返回的url content
是上一次MainThread启动的thread2返回的url content
用的是jre1.6
哪位高手给指点指点吧,谢谢
MainThread:
Thread thread1 = new Thread(new multiThread11());
Thread threa
avatar
m*t
2
interrupt() only interrupts threads that are wait()'ing or sleep()'ing.
There are timeout values you can set on a URLConnection. Also thread2 needs
to be coded to cooperate with an "interrupt" request from main.

【在 b*********n 的大作中提到】
: 我在MainThread里同时start了两个thread: thread1, thread2
: MainThread里等了2sec之后,想要kill thread2
: thread2是去obtain一个url 的content,
: 一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
: 想要实现在2sec之后,
: 如果thread2没结束就强行结束,并返回nothing,
: 如果thread2结束了就返回url content.
: 用了:thread2.interrupt() 来强行结束thread2;
: 但是好像没有用呀,thread2还是在运行,并没有end
: 而且在我连续调用MainThread时,下一次的MainThread返回的url content

avatar
h*o
3
since you are using jre 1.6, using the new thread framework introduced from
1.5 would be a better idea.

You might want to have a look at futuretask, callable, executor framework,
etc. the new framework is much better than the old one in aspects of value
return, cancellation, interruption, and exception handling, etc.
As I remember, you should be able to find the code sample to your question
in the book of Java Concurrency... (sorry, forget the full name). The book
was written by the lead desig
avatar
b*y
4


【在 b*********n 的大作中提到】
: 我在MainThread里同时start了两个thread: thread1, thread2
: MainThread里等了2sec之后,想要kill thread2
: thread2是去obtain一个url 的content,
: 一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
: 想要实现在2sec之后,
: 如果thread2没结束就强行结束,并返回nothing,
: 如果thread2结束了就返回url content.
: 用了:thread2.interrupt() 来强行结束thread2;
: 但是好像没有用呀,thread2还是在运行,并没有end
: 而且在我连续调用MainThread时,下一次的MainThread返回的url content

avatar
b*y
5
You should use a flag, say, boolean stopThread = false, it's shared by both
main thread and thread2; thread2 keep polling it in its run() method,once
the flag's status changes, thread2 exits run() and terminates itself.
MainThread is to toggle the flag after 2 seconds.
However, execise with great caution, the toggling and checking the status of
the flag, is to be guarded by synchronization to ensure the mutual
exclusion and inter-thread communication.
Please refer to "Effective Java" 2nd edition

【在 b*********n 的大作中提到】
: 我在MainThread里同时start了两个thread: thread1, thread2
: MainThread里等了2sec之后,想要kill thread2
: thread2是去obtain一个url 的content,
: 一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
: 想要实现在2sec之后,
: 如果thread2没结束就强行结束,并返回nothing,
: 如果thread2结束了就返回url content.
: 用了:thread2.interrupt() 来强行结束thread2;
: 但是好像没有用呀,thread2还是在运行,并没有end
: 而且在我连续调用MainThread时,下一次的MainThread返回的url content

avatar
k*r
6
Sounds like thread2 is blocking so it won't be able to
poll.
Another idea is for the main thread to check the result
in thread2 after 2 seconds. If it's not there, use null,
and let thread2 continue to run to finish, and ignore
the result.

both
method,once
status of

【在 b*********y 的大作中提到】
: You should use a flag, say, boolean stopThread = false, it's shared by both
: main thread and thread2; thread2 keep polling it in its run() method,once
: the flag's status changes, thread2 exits run() and terminates itself.
: MainThread is to toggle the flag after 2 seconds.
: However, execise with great caution, the toggling and checking the status of
: the flag, is to be guarded by synchronization to ensure the mutual
: exclusion and inter-thread communication.
: Please refer to "Effective Java" 2nd edition

avatar
g*g
7
I think the better way is to use FutureTask

【在 b*********n 的大作中提到】
: 我在MainThread里同时start了两个thread: thread1, thread2
: MainThread里等了2sec之后,想要kill thread2
: thread2是去obtain一个url 的content,
: 一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
: 想要实现在2sec之后,
: 如果thread2没结束就强行结束,并返回nothing,
: 如果thread2结束了就返回url content.
: 用了:thread2.interrupt() 来强行结束thread2;
: 但是好像没有用呀,thread2还是在运行,并没有end
: 而且在我连续调用MainThread时,下一次的MainThread返回的url content

avatar
Z*e
8
Thread#interrupt is discouraged, and the right way is to ask thread2 exit
willfully via some flag. The core issue here is that the input stream on URL
object might block, so what you need is a timeout on the read.
see URLConnection class, which has timeout for both connect and read, and
you can obtain an instace from URL#openConnection.

【在 b*********n 的大作中提到】
: 我在MainThread里同时start了两个thread: thread1, thread2
: MainThread里等了2sec之后,想要kill thread2
: thread2是去obtain一个url 的content,
: 一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
: 想要实现在2sec之后,
: 如果thread2没结束就强行结束,并返回nothing,
: 如果thread2结束了就返回url content.
: 用了:thread2.interrupt() 来强行结束thread2;
: 但是好像没有用呀,thread2还是在运行,并没有end
: 而且在我连续调用MainThread时,下一次的MainThread返回的url content

avatar
m*t
9
If thread2 is stuck in native socket code -- e.g.
trying to connect to the url, there is no (java)
way to interrupt it. The only possibility AFAICS
would be to start it as a daemon thread.
I hope everybody realizes that this was a thread
from over a year ago, by the way. 8-)
avatar
Z*e
10

晕!!!!

【在 m******t 的大作中提到】
: If thread2 is stuck in native socket code -- e.g.
: trying to connect to the url, there is no (java)
: way to interrupt it. The only possibility AFAICS
: would be to start it as a daemon thread.
: I hope everybody realizes that this was a thread
: from over a year ago, by the way. 8-)

avatar
F*n
11
你是第二次回答这问题吧,我看那个SOCKET,DAEMON啥的很眼熟啊:))

【在 m******t 的大作中提到】
: If thread2 is stuck in native socket code -- e.g.
: trying to connect to the url, there is no (java)
: way to interrupt it. The only possibility AFAICS
: would be to start it as a daemon thread.
: I hope everybody realizes that this was a thread
: from over a year ago, by the way. 8-)

avatar
m*t
12

This is all part of a deliberate joke/conspiracy I think...

【在 F****n 的大作中提到】
: 你是第二次回答这问题吧,我看那个SOCKET,DAEMON啥的很眼熟啊:))
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。