栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用线程的ID挂起线程?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用线程的ID挂起线程?

这些天杀死线程的正确方法就是使用

interrupt()
它。这集
Thread.isInterrupted()
真和原因
wait()
sleep()
以及一些其他的方法来扔
InterruptedException

在线程代码内部,您应该执行类似以下的操作,检查以确保它没有被中断。

 // run our thread while we have not been interrupted while (!Thread.currentThread().isInterrupted()) {     // do your thread processing pre ... }

这是一个如何在线程内部处理中断异常的示例:

 try {     Thread.sleep(...); } catch (InterruptedException e) {     // always good practice because catching the exception clears the flag     Thread.currentThread().interrupt();     // most likely we should stop the thread if we are interrupted     return; }

挂起线程的正确方法要困难一些。您可以

volatile booleansuspended
为它要注意的线程设置某种标志。然后
wait()
,您可以使用和
notify()
重新启动线程。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号