中断线程
任何人都可以告诉我们使用线程中断方法的目的是什么。为什么我们在执行过程中调用中断方法?
谢谢
Can any body tell what is the purpose of using interrupt method of thread. Why we call interrupt method during execution??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用中断方法来中断线程如果它处于阻塞状态...通常您会中断处于阻塞状态的线程,因为它要么花费太长时间,要么您的系统正在关闭,并且您想要取消该线程正在执行的任何操作。中断提供了一种“优雅”的方式来终止阻塞线程,因为它会引发中断异常,并且假定您的线程正在处理该异常。当您的线程收到中断异常时,它应该正常终止(即清理状态并退出任何循环)。
PS 你使用什么编程语言?
You call the interrupt method to interrupt the thread if it's in a blocking state... usually you interrupt a thread that's in a blocking state because it's either taking too long or your system is shutting down and you want to cancel whatever that thread is doing. Interrupt offers a "graceful" way to terminate a blocking thread, because it raises an interrupt exception and it's presumed that your thread is handing that exception. When your thread gets the interrupt exception, then it should gracefully terminate (i.e. clean up the state and exit any loops).
P.S. What programming language are you using?