包含在try-catch中的Thread.Sleep的InterruptedException

时间:2019-04-19 14:38:50

标签: android

我们有一个非常奇怪的情况。在实时应用程序中,我们使用具有后台线程的服务。并且此特定代码块引发InterruptedException。它仅在安装了Android 4.2.2的设备上发生,并且是一个用户,但是多次。

这是相关代码:

@Override
public void run()
{
    while (shouldRun.get()) // AtomicBoolean which determines thread status.
    {
        try
        {
            Thread.sleep(100); // The exception is thrown here, prevented a crash.
        }
        catch (InterruptedException ignored){} // It is ignored because shouldRun
                                               // becomes false here.
    }
}

这怎么可能?它位于try-catch块中,具有特定的异常。对于那些好奇的人,这里是堆栈跟踪:

Fatal Exception: java.lang.InterruptedException
       at java.lang.VMThread.sleep(VMThread.java)
       at java.lang.Thread.sleep(Thread.java:1010)
       at java.lang.Thread.sleep(Thread.java:992)
       at com.example.ThreadHelper.run(ThreadHelper.java:314)
       at java.lang.Thread.run(Thread.java:838)

感谢您的帮助。

0 个答案:

没有答案
相关问题