由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 急问:怎么kill一个thread, thread.interrupte()不好用呀?
相关主题
NoThread concurrency这个Timer不退出是怎么回事儿?
paint()呀,repaint()呀另一个入门问题。
a Java MultiThreading questionjava 程序中的实时控制
问个HttpClient 的问题Re: Why my url can not open a connection to my servlet in a web browse
Java简直完全不可控啊!!!Re: 关于APPLET的IO问题
请教:performance issue由一个Java Bug谈起
怎么可以练习多线程编程呢?PersonalJava and Http tunneling
如何让两个socket并行执行thread请教:AXIS 1.4 webservice client using a proxy
相关话题的讨论汇总
话题: thread2话题: thread话题: mainthread话题: url话题: content
进入Java版参与讨论
1 (共1页)
b*********n
发帖数: 1258
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
m******t
发帖数: 2416
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

h*********o
发帖数: 62
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
b*********y
发帖数: 7
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

b*********y
发帖数: 7
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

k***r
发帖数: 4260
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

g*****g
发帖数: 34805
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

Z****e
发帖数: 2999
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

m******t
发帖数: 2416
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-)
Z****e
发帖数: 2999
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-)

F****n
发帖数: 3271
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-)

m******t
发帖数: 2416
12

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

【在 F****n 的大作中提到】
: 你是第二次回答这问题吧,我看那个SOCKET,DAEMON啥的很眼熟啊:))
1 (共1页)
进入Java版参与讨论
相关主题
请教:AXIS 1.4 webservice client using a proxyJava简直完全不可控啊!!!
新手请教一个问题请教:performance issue
Re: How to lock a file and detect a thread is over?怎么可以练习多线程编程呢?
[转载] 现在还有什么OS不是THREAD级调度的吗?如何让两个socket并行执行thread
NoThread concurrency这个Timer不退出是怎么回事儿?
paint()呀,repaint()呀另一个入门问题。
a Java MultiThreading questionjava 程序中的实时控制
问个HttpClient 的问题Re: Why my url can not open a connection to my servlet in a web browse
相关话题的讨论汇总
话题: thread2话题: thread话题: mainthread话题: url话题: content