Java线程休眠和中断异常 [英] Java Thread Sleep and Interrupted Exception

查看:170
本文介绍了Java线程休眠和中断异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 为什么睡眠线程需要try catch来捕获Interrupted Exception?

  2. 为什么睡眠会发出中断异常错误?
    这是我在java编程中真正想知道的两个问题
    我一直在谷歌搜索,我还没有找到一个明确的解释是为什么这两件事发生。


推荐答案

1.-因为如果中断它,线程无法完成正常执行,并且你需要抓住它,以便做好准备。
2.-因为线程等待与被中断的线程不同,所以可以恢复线程等待,但是已经中断的线程已经完成执行。

1.- Because a Thread cant complete its normal execution if you Interrupt it, and you need to catch that in order to be prepared to do something. 2.- Because a thread waiting is different from an interrupted thread, a thread waiting can be resumed, but an interrupted thread is already finish execution.

这篇关于Java线程休眠和中断异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆