什么是Java中的InterruptedException? [英] What is an InterruptedException in Java?

查看:85
本文介绍了什么是Java中的InterruptedException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中多次看到我收到了中断异常".那是什么,我该如何解决?

I have seen many times in my code that I get an Interrupted Exception. What is that, and how do I fix it?

推荐答案

因为我不知道您对 Java ,java的工作原理以及并发性,我将尝试在几乎不需要背景信息的情况下尽可能多地进行解释.

Since I don't know how much you know about Java, how java works exactly and what's concurrency, I'll try to explain as much as possible with nearly no background information needed.

首先,我们将看一下 InterruptedException .

At first we're going to take a look at Oracle's documentation of the InterruptedException.

在活动之前或活动期间,线程在等待,睡眠或以其他方式占用时抛出,并且该线程被中断.[...]

Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. [...]


这是什么意思?

在回答此问题之前,您必须基本了解

Before answering this question you have to rudimentary understand how a Thread works.

线程最初只是一段代码,可以单独管理从其他线程.它可以同时运行(请参见并发),计划运行等.

A thread is originally just a piece of code that can be managed seperately from other threads. It can run at the same time (see concurrency), run scheduled, etc.

示例

如果启动普通的Java程序,则会在自己的线程1中创建 main()方法.您所做的一切都将在此线程中执行.创建的每个类,调用的每个方法,以及基本上所有源自 main()的东西.

If you start a normal java program the main() method is created in an own thread 1. Everything you do will be executed in this thread. Every class you create, every method you call and basically everything that originated from main().

但是,如果创建一个 Thread ,它会在自己的线程2中运行.您在 run()方法中所做的所有操作都将在线程2中执行.

But if you create a Thread, it runs in an own thread 2. Everything you do in the run() method will be executed in thread 2.

如果创建新的 Thread ,会发生什么?

What happens if you create a new Thread?

join() 方法被调用.

The join() method is called.

join()方法启动线程,并执行 run().

The join() method starts the thread and run() is being executed.

不仅有没有参数的 join()方法,而且还有

There's not only the join() method with no parameters but also a join(long millis) with a parameter.

何时以及为何将其抛出?

正如拉胡尔·艾耶(Rahul Iyer)所说,有多种原因.我会挑选其中一些来给您打电话时的感觉.

As Rahul Iyer already said there's a variety of reasons. I'm picking some of them to get you a feeling when they're called.

  1. 超时

超过超时时间时,确实可以引发 InterruptedException .在调用 join(long millis)时会发生这种情况.该参数指定线程可以在给定的毫秒数内运行.当线程超过此超时时间时,它将被中断.(从769行到822行).

The InterruptedException can be indeed thrown when a timeout was exceeded. This happens when the join(long millis) is called. The parameter specifies that the thread can run for the given amount of milliseconds. When the thread exceeds this timeout it is interrupted. source (from line 769 to line 822).

  1. 操作系统正在停止线程

也许 O 运行 S 系统(Windows,Linux,Android等)决定停止该程序.为什么?为了释放资源,您的程序被误认为是病毒,硬件被关闭,用户手动将其关闭,等等.

Maybe the Operating System (Windows, Linux, Android, etc.) decided to stop the program. Why? To free resources, your program was mistaken as a virus, hardware is shutting down, the user manually closed it, etc.

  1. 线程

  1. An InterruptedException is thrown in the Thread

例如,您已连接到互联网,并且正在阅读某些内容,而互联网突然断开了连接.

For example you're connected to the internet and you're reading something and the internet disconnects suddenly.

interrupt()被称为

interrupt() is called

正如已经提到的,当您在任何 Thread 上调用 interrupt()(很合逻辑)时,也会抛出 InterruptedException ./p>

As already mentioned, an InterruptedException is also thrown, when you call the interrupt() on any Thread (quite logical).


如何处理?

您可以/应该像处理其他所有异常一样处理它.将其包装为 try catch ,声明方法 throws 并将其记录下来,等等.

You can/should handle it like every other exception. Wrap it in a try and catch, declare that the method throws it, log it, etc.

这可能是最适合您的资源:在Java中处理InterruptedException .

This may be the best resource for you: Handling InterruptedException in Java.

这篇关于什么是Java中的InterruptedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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