多线程怎么样中止

wood_ysu 2006-06-19 06:52:36
一个下载窗口中有一个按钮开始下载,一个按钮中止下载。
下载使用多线程在后台执行,
下载过程中点击中止按钮停止下载,
应该怎么样中止多个线程呢
...全文
298 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
treeroot 2006-06-20
  • 打赏
  • 举报
回复
up
suihu 2006-06-20
  • 打赏
  • 举报
回复
Mark~
johnroy 2006-06-20
  • 打赏
  • 举报
回复
Curiously, there are two very similar methods, interrupted and isInterrupted. The interrupted method is a static method that checks whether the current thread has been interrupted. (Recall that a thread is interrupted because another thread has called its interrupt method.) Furthermore, calling the interrupted method resets the "interrupted" status of the thread. On the other hand, the isInterrupted method is an instance method that you can use to check whether any thread has been interrupted. Calling it does not change the "interrupted" status of its argument.


It is a bit tedious that there are two distinct ways of dealing with thread interruption—testing the "interrupted" flag and catching the InterruptedException.

It would have been nice if methods such as sleep had been defined to simply return with the "interrupted" flag set when an interruption occurs—then one wouldn't have to deal with the InterruptedException at all. Of course, you can manually set the "interrupted" flag when an InterruptedException is caught:

try
{
sleep(delay);
}
catch (InterruptedException exception)
{
Thread.currentThread().interrupt();
}

You need to use this approach if the sleep method is called from a method that can't throw any exceptions.

NOTE


You'll find lots of published code where the InterruptedException is squelched, like this:

try { sleep(delay); }
catch (InterruptedException exception) {} // DON'T!

Don't do that! Either set the "interrupted" flag of the current thread, or propagate the exception to the calling method (and ultimately to the run method).


If you don't want to clutter up lots of nested methods with isInterrupted tests, you can turn the "interrupted" flag into an exception.

if (isInterrupted()) throw new InterruptedException();

Assuming that your code is already prepared to terminate the run method when an InterruptedException is thrown, this is a painless way of immediately terminating the thread when an interruption is detected. The principal disadvantage is that you have to tag your methods with

throws InterruptedException

since, alas, the InterruptedException is a checked exception.

做鸡真好吃 2006-06-20
  • 打赏
  • 举报
回复
Mark~
wood_ysu 2006-06-20
  • 打赏
  • 举报
回复
多谢楼上的。目前我急切想知道的是具体代码编写。
我在run方法中使用while(!quit){ ... }包含住所有代码,
中断的时候把quit设置为true。这样就不会进入执行代码了。
可实际运行结果不是这样。里边的代码还在执行。
不知道应该怎样停止它?
ajohnsun 2006-06-20
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4830/4830683.xml?temp=3.413028E-02
看看!
wood_ysu 2006-06-20
  • 打赏
  • 举报
回复
kexile
awolf168 2006-06-20
  • 打赏
  • 举报
回复
up
smlovetp 2006-06-20
  • 打赏
  • 举报
回复
接分
Trainsp0tting 2006-06-20
  • 打赏
  • 举报
回复
mark
ttaallkk1 2006-06-19
  • 打赏
  • 举报
回复
[线程的interrupt()方法,interrupted()和isInterrupted()]

这三个方法是关系非常密切而且又比较复杂的,虽然它们各自的功能很清楚,但它们之间的关系有大多数
人不是真正的了解.

先说interrupt()方法,它是实例方法,而它也是最奇怪的方法,在java语言中,线程最初被设计为"隐晦难懂"
的东西,直到现在它的语义不没有象它的名字那样准确.
大多数人以为,一个线程象调用了interrupt()方法,那它对应的线程就应该被中断而抛出异常,
事实中,当一个线程对象调用interrupt()方法,它对应的线程并没有被中断,只是改变了它的中断状态.
使当前线程的状态变以中断状态,如果没有其它影响,线程还会自己继续执行.
只有当线程执行到sleep,wait,join等方法时,或者自己检查中断状态而抛出异常的情况下,线程
才会抛出异常.

如果线程对象调用interrupt()后它对应的线程就立即中断,那么interrupted()方法就不可能执行.
因为interrupted()方法是一个static方法,就是说只能在当前线程上调用,而如果一个线程interrupt()后
它已经中断了,那它又如何让自己interrupted()?

正因为一个线程调用interrupt()后只是改变了中断状态,它可以继续执行下去,在没有调用sleep,
wait,join等法或自己抛出异常之前,它就可以调用interrupted()来清除中断状态(还会原状)
interrupted()方法会检查当前线程的中断状态,如果为"被中断状态"则改变当前线程为"非中断状
态"并返回true,如果为"非中断状态"则返回false,它不仅检查当前线程是否为中断状态,而且在保证当前线
程回来非中断状态,所以它叫"interrupted",是说中断的状态已经结束(到非中断状态了)

isInterrupted()方法则仅仅检查线程对象对应的线程是否是中断状态,并不改变它的状态.

目前大家只能先记住这三个方法的功能,只有真正深入到多线程编程实践中,才会体会到它们为什么
是对象方法,为什么是类方法.

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧