java 需要示例程序抛出 InterruptedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3642362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:47:53  来源:igfitidea点击:

Need sample program to throw InterruptedException

javamultithreading

提问by javanoob

I am going through the kathy sierra SCJP 1.5 Chapter 9(threads)and there it is mentioned as:

我正在阅读kathy sierra SCJP 1.5 Chapter 9(threads),其中提到:

Notice that the sleep() method can throw a checked InterruptedException (you'll usually know if that is a possibility, since another thread has to explicitly do the interrupting), so you must acknowledge the exception with a handle or declare

请注意, sleep() 方法可以抛出一个检查过的 InterruptedException (您通常会知道这是否可能,因为另一个线程必须显式执行中断),因此您必须使用句柄或声明来确认异常

I just need a sample program to know when it happens (which i can run on my machine)?

我只需要一个示例程序来知道它何时发生(我可以在我的机器上运行)?

I googled but could not find any sample code to test this functionality..

我用谷歌搜索但找不到任何示例代码来测试此功能..

Thanks in Advance

提前致谢

回答by Jon Skeet

Here's an example:

下面是一个例子:

public class Test
{
    public static void main (String[] args)
    {
        final Thread mainThread = Thread.currentThread();

        Thread interruptingThread = new Thread(new Runnable() {
            @Override public void run() {
                // Let the main thread start to sleep
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                mainThread.interrupt();
            }
        });

        interruptingThread.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            System.out.println("I was interrupted!");
        }
    }
}

To walk through it:

要遍历它:

  • Set up a new thread which will sleep for a short time, then interrupt the main thread
  • Start that new thread
  • Sleep for a long-ish time (in the main thread)
  • Print out a diagnostic method when we're interrupted (again, in the main thread)
  • 建立一个新线程,它会休眠一小段时间,然后中断主线程
  • 开始那个新线程
  • 长时间休眠(在主线程中)
  • 当我们被中断时打印出诊断方法(再次,在主线程中)

The sleep in the main thread isn't strictlynecessary, but it means that the main thread does get to really start sleeping before it's interrupted.

主线程中的睡眠并不是绝对必要的,但这意味着主线程确实会在被中断之前真正开始睡眠。

回答by user6773775

public class SleepTest1 extends Thread {
@Override
public void run() {
    try {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(1000);
            Thread.currentThread().interrupt();
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    SleepTest1 st1 = new SleepTest1();
    st1.start();
}

}

}