什么样的行为导致中断的异常? [英] What kind of behaviour causes an interrupted exception?

查看:194
本文介绍了什么样的行为导致中断的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中使用线程相对较新,我注意到每次使用Thread.sleep()时,都必须捕获InterrupetdException。



什么样的行为会导致这种情况,在简单的应用程序中,我有一个监视器线程可以忽略异常?

解决方案

线程调用thread.interupt(),当线程正在睡觉时,你会得到异常。是的,你可能只是把try..catch放在sleep()并忽略它;)


I'm relatively new to Threading in Java and I've noticed that everytime I use Thread.sleep() I have to catch InterrupetdException.

What kind of behaviour causes this, and in simple applications where I have a monitor thread can I just Ignore the exception?

解决方案

Well if some other Thread calls thread.interupt(), while the thread is sleeping, you'll get the Exception. And yes, you can probably just put try..catch arround the sleep() and ignore it ;)

这篇关于什么样的行为导致中断的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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