美文网首页
InterruptedException

InterruptedException

作者: 陈萍儿Candy | 来源:发表于2020-10-27 10:24 被阅读0次

错误

System.err: java.lang.InterruptedException
2020-10-26 20:07:36.303 20821-21161/com.example.uxin.myapplication W/System.err:     at java.lang.Thread.sleep(Native Method)
2020-10-26 20:07:36.303 20821-21161/com.example.uxin.myapplication W/System.err:     at java.lang.Thread.sleep(Thread.java:373)
2020-10-26 20:07:36.303 20821-21161/com.example.uxin.myapplication W/System.err:     at java.lang.Thread.sleep(Thread.java:314)
2020-10-26 20:07:36.303 20821-21161/com.example.uxin.myapplication W/System.err:     at threadtest.ThreadTest1.run(ThreadTest1.java:49)

测试代码
线程:

class ThreadTest1  extends Thread{
    private LockObject lockObject ;

    public ThreadTest2(LockObject lockObject) {
        this.lockObject = lockObject;
    }

    @Override
    public void run() {
        super.run();
        try {
            Log.i("cyp", "Thread1   sleep 方法kaishi"+this.getState());
            sleep(20000);
            Log.i("cyp", "Thread1   sleep 方法jieshu"+this.getState());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

activity的oncreate方法中

        lockObject = new LockObject();
        thread1 = new ThreadTest1(lockObject);
        thread1.setPriority(5);
        thread1.start();
        btSleep.postDelayed(new Runnable() {
            @Override
            public void run() {
              // 2秒后中断线程thread1
                thread1.interrupt();
                Log.i(TAG, "onCreate: "+thread1.isInterrupted());
                Log.i(TAG, "onCreate: "+thread1.interrupted());
            }
        },2000);

运行代码后出现InterruptedException异常,当时线程正在执行sleep,中断出现异常;

InterruptedException源码:

/**
 * 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.  The following code can be used to achieve
 * this effect:
 * <pre>
 *  if (Thread.interrupted())  // Clears interrupted status!
 *      throw new InterruptedException();
 * </pre>
 *
 * @author  Frank Yellin
 * @see     java.lang.Object#wait()
 * @see     java.lang.Object#wait(long)
 * @see     java.lang.Object#wait(long, int)
 * @see     java.lang.Thread#sleep(long)
 * @see     java.lang.Thread#interrupt()
 * @see     java.lang.Thread#interrupted()
 * @since   JDK1.0
 */
public
class InterruptedException extends Exception {
    private static final long serialVersionUID = 6700697376100628473L;

    /**
     * Constructs an <code>InterruptedException</code> with no detail  message.
     */
    public InterruptedException() {
        super();
    }

    /**
     * Constructs an <code>InterruptedException</code> with the
     * specified detail message.
     *
     * @param   s   the detail message.
     */
    public InterruptedException(String s) {
        super(s);
    }
}

相关文章

网友评论

      本文标题:InterruptedException

      本文链接:https://www.haomeiwen.com/subject/fqszmktx.html