Java正确处理InterruptedException的方法
2014-07-17 10:56
288 查看
要想讨论正确处理InterrupedtException的方法,就要知道InterruptedException是什么。
根据Java Doc的定义
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw
this exception.
意思是说当一个线程处于等待,睡眠,或者占用,也就是说阻塞状态,而这时线程被中断就会抛出这类错误。Java6之后结束某个线程A的方法是A.interrupt()。如果这个线程正处于非阻塞状态,比如说线程正在执行某些代码的时候,不过被interrupt,那么该线程的interrupt变量会被置为true,告诉别人说这个线程被中断了(只是一个标志位,这个变量本身并不影响线程的中断与否),而且线程会被中断,这时不会有interruptedException。但如果这时线程被阻塞了,比如说正在睡眠,那么就会抛出这个错误。请注意,这个时候变量interrupt没有被置为true,而且也没有人来中断这个线程。比如如下的代码:
[java]
view plaincopyprint?
while(true){
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
logger.error("thread interrupted",ex);
}
}
当线程执行sleep(1000)之后会被立即阻塞,如果在阻塞时外面调用interrupt来中断这个线程,那么就会执行
[java]
view plaincopyprint?
logger.error("thread interrupted",ex);
这个时候其实线程并未中断,执行完这条语句之后线程会继续执行while循环,开始sleep,所以说如果没有对InterruptedException进行处理,后果就是线程可能无法中断
所以,在任何时候碰到InterruptedException,都要手动把自己这个线程中断。由于这个时候已经处于非阻塞状态,所以可以正常中断,最正确的代码如下:
[java]
view plaincopyprint?
while(!Thread.isInterrupted()){
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
Thread.interrupt()
}
}
这样可以保证线程一定能够被及时中断。
对于更为复杂的情况,除了要把自己的线程中断之外,还有可能需要抛出InterruptedException给上一层代码
根据Java Doc的定义
Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw
this exception.
意思是说当一个线程处于等待,睡眠,或者占用,也就是说阻塞状态,而这时线程被中断就会抛出这类错误。Java6之后结束某个线程A的方法是A.interrupt()。如果这个线程正处于非阻塞状态,比如说线程正在执行某些代码的时候,不过被interrupt,那么该线程的interrupt变量会被置为true,告诉别人说这个线程被中断了(只是一个标志位,这个变量本身并不影响线程的中断与否),而且线程会被中断,这时不会有interruptedException。但如果这时线程被阻塞了,比如说正在睡眠,那么就会抛出这个错误。请注意,这个时候变量interrupt没有被置为true,而且也没有人来中断这个线程。比如如下的代码:
[java]
view plaincopyprint?
while(true){
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
logger.error("thread interrupted",ex);
}
}
<span style="font-family:KaiTi_GB2312;font-size:18px;">while(true){ try { Thread.sleep(1000); }catch(InterruptedException ex) { logger.error("thread interrupted",ex); } }</span>
当线程执行sleep(1000)之后会被立即阻塞,如果在阻塞时外面调用interrupt来中断这个线程,那么就会执行
[java]
view plaincopyprint?
logger.error("thread interrupted",ex);
<span style="font-family:KaiTi_GB2312;font-size:18px;">logger.error("thread interrupted",ex);</span>
这个时候其实线程并未中断,执行完这条语句之后线程会继续执行while循环,开始sleep,所以说如果没有对InterruptedException进行处理,后果就是线程可能无法中断
所以,在任何时候碰到InterruptedException,都要手动把自己这个线程中断。由于这个时候已经处于非阻塞状态,所以可以正常中断,最正确的代码如下:
[java]
view plaincopyprint?
while(!Thread.isInterrupted()){
try {
Thread.sleep(1000);
}catch(InterruptedException ex)
{
Thread.interrupt()
}
}
<span style="font-family:KaiTi_GB2312;font-size:18px;">while(!Thread.isInterrupted()){ try { Thread.sleep(1000); }catch(InterruptedException ex) { Thread.interrupt() } }</span>
这样可以保证线程一定能够被及时中断。
对于更为复杂的情况,除了要把自己的线程中断之外,还有可能需要抛出InterruptedException给上一层代码
相关文章推荐
- Java正确处理InterruptedException的方法
- Java正确处理InterruptedException的方法
- 正确使用MySQL JDBC setFetchSize() setMaxRows()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- Java 导入包正确依旧报错的处理方法
- Java 导入包正确依旧报错的处理方法
- Java 导入包正确依旧报错的处理方法
- Java中split方法对结尾空字符串不正确处理的解决方案
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- Java 导入包正确依旧报错的处理方法
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryEr
- Java正确处理InterruptedException的方法(java并发编程第7章)
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- Java正确处理InterruptedException的方法
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果集 java.lang.OutOfMemoryError: Java heap space
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- java传入方法的值在处理后方法外还能拿到新值
- JAVA 处理时间 - java.sql.Date、java.util.Date与数据库中的Date字段的转换方法
- 正确处理SqlCeException的方法
- Java日期时间处理的简要使用方法