`
coolxing
  • 浏览: 876563 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
9a45b66b-c585-3a35-8680-2e466b75e3f8
Java Concurre...
浏览量:98110
社区版块
存档分类
最新评论

处理InterruptedException异常--JCIP7.1读书笔记

阅读更多

[本文是我对Java Concurrency In Practice 7.1的归纳和总结.  转载请注明作者和出处,  如有谬误, 欢迎在评论中指正. ]

 

在java的中断机制中, InterruptedException异常占据重要的位置. 处理InterruptedException异常的方式有:

 

1. 不catch直接向上层抛出, 或者catch住做一些清理工作之后重抛该异常. 这样的处理使得你的方法也成为一个可中断的阻塞方法:

// 直接向上层抛出InterruptedException, dosomething方法也是一个可中断的阻塞方法
private void dosomething() throws InterruptedException {
	Thread.sleep(1000);
}
  

2. 有时不能向上抛出InterruptedException异常(例如父类的相应方法没有声明该异常), 此时catch之后, 必须设置当前线程的中断标记为true, 以表明当前线程发生了中断, 以便调用栈上层进行处理:

public class InterruptedExceptionHandler implements Runnable {
	private Object lock = new Object();

	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			dosomething();
		}
	}

	private void dosomething() {
		try {
			// Object.wait是一个可中断的阻塞方法, 如果在其阻塞期间检查到当前线程的中断标记为true, 会重置中断标记后从阻塞状态返回, 并抛出InterruptedException异常
			synchronized (lock) {
				lock.wait();
			}
		} catch (InterruptedException e) {
			System.out.println("InterruptedException happened");
			// catch住InterruptedException后设置当前线程的中断标记为true, 以供调用栈上层进行相应的处理
			// 在此例中, dosomething方法的调用栈上层是run方法.
			Thread.currentThread().interrupt();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(new InterruptedExceptionHandler());
		t.start();
		// 启动线程1s后设置其中断标记为true
		Thread.sleep(1000);
		t.interrupt();
	}
} 

主线程启动InterruptedExceptionHandler线程1s后, 设置InterruptedExceptionHandler线程的中断标记为true. 此时InterruptedExceptionHandler线程应该阻塞在wait方法上, 由于wait方法是可中断的阻塞方法, 所以其检查到中断标记为true时, 将重置当前线程的中断标记后抛出InterruptedException, dosomething方法catch住InterruptedException异常后, 再次将当前线程的中断标记设置为true, run方法检查到中断标记为true, 循环不再继续. 假如dosomething方法catch住InterruptedException异常后没有设置中断标记, 其调用栈上层的run方法就无法得知线程曾经发生过中断, 循环也就无法终止.

 

3. 还有一种情形比较特殊: 我们希望发生了InterruptedException异常后仍然继续循环执行某阻塞方法, 此时应该将中断状态保存下来, 当循环完成后再根据保存下来的中断状态执行相应的操作:

public class InterruptedExceptionContinueHandler implements Runnable {
	private BlockingQueue<Integer> queue;

	public InterruptedExceptionContinueHandler(BlockingQueue<Integer> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			dosomething();
		}
		System.out.println(queue.size());
	}

	private void dosomething() {
		// cancelled变量用于表明线程是否发生过中断
		boolean cancelled = false;
		for (int i = 0; i < 10000; i++) {
			try {
				queue.put(i);
			} catch (InterruptedException e) {
				// 就算发生了InterruptedException, 循环也希望继续运行下去, 此时将cancelled设置为true, 以表明遍历过程中发生了中断
				System.out.println("InterruptedException happened when i = " + i);
				cancelled = true;
			}
		}
		// 如果当前线程曾经发生过中断, 就将其中断标记设置为true, 以通知dosomething方法的上层调用栈
		if (cancelled) {
			Thread.currentThread().interrupt();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(new InterruptedExceptionContinueHandler(new LinkedBlockingQueue<Integer>()));
		t.start();
		
		// 启动线程2ms后设置其中断标记为true
		Thread.sleep(2);
		t.interrupt();
	}
}

在我的机器中, 输出结果如下:

InterruptedException happened when i = 936

size = 9999

队列的size是9999而不是10000, 是因为i = 936时发生了InterruptedException异常, 该次put没有成功.

为什么不在发生InterruptedException时就设置当前线程的中断标记, 而非要绕一圈? 假设将dosomething方法改为:

private void dosomething() {
	for (int i = 0; i < 10000; i++) {
		try {
			queue.put(i);
		} catch (InterruptedException e) {
			System.out.println("InterruptedException happened when i = " + i);
			Thread.currentThread().interrupt();
		}
	}
}

运行后发现结果类似为:

InterruptedException happened when i = 936

InterruptedException happened when i = 937

...

InterruptedException happened when i = 9998

InterruptedException happened when i = 9999

size = 936

catch住InterruptedException后立即将当前线程的中断标记设置为true, 就会导致put方法又抛出InterruptedException异常, 如此往复直到循环结束.

 

4. 最不可取的是catch了InterruptedException异常但是不做任何处理, 这样一来调用栈上层就无法得知当前线程是否发生过中断. 只有一种情况下可以这样处理: 当InterruptedException发生在调用栈的最上层, 如run方法, 或者main方法中, 且后续代码不检查中断状态时:

public static void main(String[] args) {
	// main方法已经是调用栈的最上层, 此时可以catchInterruptedException后不做任何处理
	try {
		Thread.sleep(2);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}
1
0
分享到:
评论

相关推荐

    Java处理InterruptedException异常的理论与实践

    在Java编程中,`InterruptedException`是一个特殊的异常类型,主要用于处理线程中断的情况。当你在代码中调用诸如`Thread.sleep()`, `Thread.join()`, 或 `Object.wait()`等阻塞方法时,如果线程被中断,这些方法会...

    InterruptedException.md

    - 在捕获到InterruptedException后,可以执行一些清理工作,并选择是否重新抛出该异常、忽略该异常,或者根据应用程序的逻辑进行其他类型的处理。 - finally块中的代码无论是否发生异常都会执行,常用在finally块...

    InterruptedException如何解决.md

    ### InterruptedException异常处理方法一:捕获并处理异常 在多线程编程中,当一个线程正在执行阻塞操作(如线程睡眠Thread.sleep()或等待锁等)时,如果其他线程通过中断方式要求停止阻塞操作,此时被中断的线程会...

    处理 InterruptedException1

    InterruptedException 是一种检查异常(checked exception),不能被忽略,因为它提供了一个机会来正确地处理中断请求。 中断机制是 Java 语言中的一种协作机制,它允许一个线程请求另一个线程停止正在做的事情。当...

    InterruptedException(解决方案).md

    因此,如果线程正在等待状态,但被中断,则不会继续等待,而是进入catch块处理异常。 6. InterruptedException的最佳实践:在实际编程中,应尽量使用中断机制来控制线程的中断,而不是使用boolean标志。处理...

    java.lang.InterruptedException(解决方案).md

    处理InterruptedException异常主要涉及到两方面:一方面是确保任务线程内部能够正确捕获并处理异常,另一方面是在其他线程中合理地发起中断操作。 在处理InterruptedException时,首先要了解它是由Java中断机制触发...

    java异常(pdf格式)

    3. **异常的声明**:当一个方法可能会抛出异常,但不想在该方法内部处理时,可以通过在方法签名中声明抛出异常的方式来告诉调用者需要处理这些异常。 #### 五、常见异常类型 1. **运行时异常**: - `Arithmetic...

    InterruptedException解决办法.md

    通过合理捕获和处理异常,可以让程序更加健壮,同时也能提升程序处理中断的灵活性和可靠性。在多线程环境中,确保线程响应中断,并按照中断意图来合理安排后续操作,对于保证应用的稳定性和可靠性至关重要。

    Java第二十九——三十一讲总结-异常处理.doc

    ### Java异常处理详解 #### 一、异常的概念与分类 在Java编程中,**异常**是一种用来处理程序运行过程中可能出现的错误或者不寻常情况的机制。异常不仅可以帮助开发者捕获和处理错误,还可以用于实现一些特殊的...

    JAVA InterruptedException

    1. **重新抛出异常**:如果你的代码不负责处理中断,可以选择将`InterruptedException`重新抛出,让上层调用者处理。 2. **捕获并处理**:如果你需要做一些清理工作,比如关闭文件流或释放资源,可以在`catch`块中...

    Java笔记高级部分20190819.pdf

    `catch`块用于捕获和处理异常,可以有一个或多个`catch`块。`finally`块则是无论是否发生异常都会执行的代码块,通常用于资源释放。 ```java try { // 可能抛出异常的代码 } catch (ExceptionType1 e1) { // ...

    Java异常类型-Java教程共1页.pdf.zip

    Java异常处理是编程中至关重要的一个环节,它确保了程序在遇到错误时能够优雅地运行,而不是突然崩溃。本教程将深入探讨Java异常的类型及其处理机制。 在Java中,异常是程序执行过程中发生的错误,可以是逻辑错误、...

    java异常类的学习(含题目与答案)

    - **掌握异常处理的编程特点**:了解如何在Java中处理程序运行时出现的各种异常情况,包括如何使用`try-catch-finally`结构来捕获并处理异常。 - **了解Java异常分类层次,常见系统异常**:熟悉Java异常类的层次结构...

    System.Threading.ThreadInterruptedException(解决方案).md

    在提供的示例代码中,演示了如何创建一个新线程,并在其内部处理InterruptedException异常。代码中模拟了一个长时间运行的操作,该操作会阻塞当前线程,直到线程被中断。主线程通过调用`interrupt()`方法来中断子...

Global site tag (gtag.js) - Google Analytics