精华内容
下载资源
问答
  • interrupt

    2019-04-29 22:43:21
    interrupt 首先梳理Thread关于interrupt的定义 /** * Interrupts this thread. * * <p> Unless the current thread is interrupting itself, which is * always permitted, the {@link #checkAccess() ...

    interrupt

    首先梳理Thread关于interrupt的定义

    /**
     * Interrupts this thread.
     *
     * <p> Unless the current thread is interrupting itself, which is
     * always permitted, the {@link #checkAccess() checkAccess} method
     * of this thread is invoked, which may cause a {@link
     * SecurityException} to be thrown.
     *
     * <p> If this thread is blocked in an invocation of the {@link
     * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
     * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
     * class, or of the {@link #join()}, {@link #join(long)}, {@link
     * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
     * methods of this class, then its interrupt status will be cleared and it
     * will receive an {@link InterruptedException}.
     *
     * <p> If this thread is blocked in an I/O operation upon an {@link
     * java.nio.channels.InterruptibleChannel InterruptibleChannel}
     * then the channel will be closed, the thread's interrupt
     * status will be set, and the thread will receive a {@link
     * java.nio.channels.ClosedByInterruptException}.
     *
     * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
     * then the thread's interrupt status will be set and it will return
     * immediately from the selection operation, possibly with a non-zero
     * value, just as if the selector's {@link
     * java.nio.channels.Selector#wakeup wakeup} method were invoked.
     *
     * <p> If none of the previous conditions hold then this thread's interrupt
     * status will be set. </p>
     *
     * <p> Interrupting a thread that is not alive need not have any effect.
     *
     * @throws  SecurityException
     *          if the current thread cannot modify this thread
     *
     * @revised 6.0
     * @spec JSR-51
     */
    public void interrupt() 
    

    这里讲述了Thread实例对象调用方法interrupt,有这几种情况

    ​ 1.在线程里面有wait,sleep,join方法,会弹出InterruptedException,状态会被清除

    ​ 2.io里面,会弹出异常,状态会被设置,…

    ​ 3.如果线程阻塞在nio中的Selector中,状态会被设置,…

    ​ 4.如果不是上面,这些情况,状态会被设置

    可以看出interrupt是关于状态的设置

    我们来验证一下

    第一种,没有任何情况的

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
           while(true){
               if (interrupted()) {
                   break;
               }
               System.out.println("i" + new Date());
           }
        });
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
    

    线程会在(1000+)ms后结束,整个程序也会结束

    第二种,有sleep的情况下

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
           while(true){
               if (interrupted()) {
                   break;
               }
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
    
               System.out.println("i" + new Date());
           }
        });
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
    

    方法并不会退出,只是打出了一个InterruptException

    作者:大闲人柴毛毛

    链接:https://www.zhihu.com/question/41048032/answer/252905837

    来源:知乎

    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    如何处理中断?

    上文都在介绍如何获取中断状态,那么当我们捕获到中断状态后,究竟如何处理呢?

    • Java类库中提供的一些可能会发生阻塞的方法都会抛InterruptedException异常,如:BlockingQueue#put、BlockingQueue#take、Object#wait、Thread#sleep。
    • 当你在某一条线程中调用这些方法时,这个方法可能会被阻塞很长时间,你可以在别的线程中调用当前线程对象的interrupt方法触发这些函数抛出InterruptedException异常。
    • 当一个函数抛出InterruptedException异常时,表示这个方法阻塞的时间太久了,别人不想等它执行结束了。
    • 当你的捕获到一个InterruptedException异常后,亦可以处理它,或者向上抛出。
    • 抛出时要注意???:当你捕获到InterruptedException异常后,当前线程的中断状态已经被修改为false(表示线程未被中断);此时你若能够处理中断,则不用理会该值;但如果你继续向上抛InterruptedException异常,你需要再次调用interrupt方法,将当前线程的中断状态设为true。
    • 注意:绝对不能“吞掉中断”!即捕获了InterruptedException而不作任何处理。这样违背了中断机制的规则,别人想让你线程中断,然而你自己不处理,也不将中断请求告诉调用者,调用者一直以为没有中断请求。

    更新相关interrupt方法的意义

    public void interrupt()

    Thread里面的打断函数,可以使用新建一个Thread对象,执行thread.interrupt();

    修改调用线程的interrupt 的status

    public static boolean interrupted()

    该方法可以判断是否被打断,但是这个interrupt的status会被清除,

    /**
         * Tests whether the current thread has been interrupted.  The
         * <i>interrupted status</i> of the thread is cleared by this method.  In
         * other words, if this method were to be called twice in succession, the
         * second call would return false (unless the current thread were
         * interrupted again, after the first call had cleared its interrupted
         * status and before the second call had examined it).
         *
         * <p>A thread interruption ignored because a thread was not alive
         * at the time of the interrupt will be reflected by this method
         * returning false.
         *
         * @return  <code>true</code> if the current thread has been interrupted;
         *          <code>false</code> otherwise.
         * @see #isInterrupted()
         * @revised 6.0
         */
    

    public boolean isInterrupted()

    该方法可以判断是否被打断

    展开全文
  • Interrupt

    2018-12-18 16:01:52
    在JDK之前的版本中可以通过stop、suspend来终止、中断线程的指向,不过...目前推荐的方式是通过interrupt来实现。 一源码分析 1interrupt() Thread#interrupt()的作用其实也不是中断线程,而是通知线程应该中断...


    在JDK之前的版本中可以通过stop、suspend来终止、中断线程的指向,不过目前这些方法已经不建议使用了,那如果希望在执行到某一条件时中断线程的执行,应该怎么做呢?目前推荐的方式是通过interrupt来实现。

     

     源码分析

    1  interrupt()

    Thread#interrupt()的作用其实也不是中断线程,而是通知线程应该中断了,具体来说,当对一个线程调用interrupt() 时:

     - 如果线程处于被阻塞状态(例如处于sleep, wait, join 等状态),那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常,仅此而已。

     - 如果线程处于正常活动状态,那么会将该线程的中断标志设置为true,仅此而已。被设置中断标志的线程将继续正常运行,不受影响。

    interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行,在正常运行任务时,经常检查本线程的中断标志位,如果被设置了中断标志就自行停止线程。具体到底中断还是继续运行,应该由被通知的线程自己处理。

        public void interrupt() {

            if (this != Thread.currentThread())

                checkAccess();

     

            synchronized (blockerLock) {

                Interruptible b = blocker;

                if (b != null) {

                    interrupt0();           // Just to set the interrupt flag

                    b.interrupt(this);

                    return;

                }

            }

            interrupt0();  // Just to set the interrupt flag

        }

     

    2  interrupted()

    检查当前中断标识(即查看当前中断信号是true还是false),并清除中断信号。一般处理过中断以后使用此方法。

        public static boolean interrupted() {

            return currentThread().isInterrupted(true);

        }

        /**

         * Tests if some Thread has been interrupted.  The interrupted state

         * is reset or not based on the value of ClearInterrupted that is

         * passed.

         */

        private native boolean isInterrupted(boolean ClearInterrupted);

     

    3  isInterrupted()

    检查当前中断标识(即查看当前中断信号是true还是false)

        public boolean isInterrupted() {

            return isInterrupted(false);

        }

     

     示例

    以下示例展示了通过isInterrupted()、interrupt()如何终止执行中的线程,如下所示代码中中创建了两个线程(thread1、thread2),thread1每隔500ms数数一次,thread2休眠一段时间以后中断thread1,thread1将会终止。

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            try {
                int i = 0;
                while (!Thread.currentThread().isInterrupted()) {
                    Thread.sleep(500);
                   System.out.println("couting... i=" + i);
                    i++;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
               System.out.println("thread1 interrupted");
            }
        });
        Thread thread2 = new Thread(() -> {
            Random random = new Random();
            try {
                long time = random.nextInt(3000) + 1000;
                Thread.sleep(time);
               System.out.println("interrupt thread1");
                thread1.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
    }

     

     

    展开全文
  • Interrupt keys

    2020-12-08 19:18:41
    <div><p>Does this have a feature that can accept multiple digits for interrupt and get the digits press? I mean for example 0123456789 can be my interrupt keys and when I press 1 this will interrupt ...
  • Interrupt Functions

    2020-12-09 10:01:10
    <div><p>Interrupt Codes: ACC_NM - Accelerometer No Motion ACC_SM - Accelerometer Slow Motion ACC_AM - Accelerometer Any Motion ACC_HIGH_G - Accelerometer High G GYR_HIGH_RATE - Gyroscope High Rate ...
  • Dockerize interrupt

    2020-12-08 23:17:51
    <p>One of the great features that come with Interrupt is the fact that there are different authors that share their experiences along a diverse set of embedded topics. I think that to write an article...
  • Interrupt callback

    2021-01-12 03:50:11
    m trying to use the method interrupt_callback from AVFormatContext, but I can't get it working. I don't know how to use the Callback_Pointer to define the interrupt function. Please, Do you ...
  • Asynchronous interrupt 中斷请求信号来自CM3内核的外面,来自各种片上外设和外扩的外设,对CM3来说是“异步”的; e.g. usb otg device Synchronous interrupt 在执行指令或访问存储器时产生,因此对CM3来说是...

    Asynchronous interrupt

    中斷请求信号来自CM3内核的外面,来自各种片上外设和外扩的外设,对CM3来说是“异步”的;
    e.g. usb otg device

    Synchronous interrupt

    在执行指令或访问存储器时产生,因此对CM3来说是“同步”的。
    e.g. assembly language
    int 31

    Reference

    http://jeremybai.github.io/blog/2014/03/15/interrupt

    转载于:https://www.cnblogs.com/youchihwang/p/7553809.html

    展开全文
  • interrupt函数

    2019-03-12 20:15:03
    调用interrupt(),通知线程应该中断了 如果线程处于被阻塞状态,那么线程立即退出被阻塞状态,并抛出一个InterruptedException异常 如果线程处于正常活动状态,那么该线程的中断标志设置为true。被设置中断标志的...

    介绍

    已经被抛弃的方法
    通过调用stop方法来停止线程

    目前使用的方法
    调用interrupt(),通知线程应该中断了

    1. 如果线程处于被阻塞状态,那么线程立即退出被阻塞状态,并抛出一个InterruptedException异常
    2. 如果线程处于正常活动状态,那么该线程的中断标志设置为true。被设置中断标志的线程将继续正常运行,不受影响。

    参考博客

    展开全文
  • The Generic Interrupt Controller (GIC) architecture defines: • the architectural requirements for handling all interrupt sources for any processor connected to a GIC • a common interrupt controller ...
  • interrupt相关

    2020-08-17 16:47:25
    interrupt && isInterrupted && interrupted && interruptException     1、interrupt:其作用是中断此线程(此线程不一定是当前线程,而是指调用该方法的Thread实例所代表的线程),但...
  • <div><p>The port interrupt pin dispatch code <code>if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1](); compiles down to the following: <pre><code> ... 16: 03a1 lsls r1, r4, #14 ...
  • Interrupt方法结束线程

    2020-12-21 20:22:54
    当调用线程的interrupt()方法时,会抛出InterruptException异常。阻塞中的那个方法抛出这个异常,通过代码捕获该异常,然后break跳出循环状态,从而让我们有机会结束这个线程的执行。通常很多人认为只要调用...
  • 理解interrupt

    2020-05-10 09:49:23
    1.thread.interrupt 中断可以理解为线程的一个标识位属性,它表示一个运行中的线程是否被其他线程进行了中断操作。 中断好比其他线程对该线程打了个招呼,其他线程通过调用该线程的interrupt()方法对其进行中断操作...
  • Rework interrupt scheduling

    2020-11-22 00:08:27
    <p>The main limitation is that the number of cycles between current cycle count and next interrupt event cannot exceed 2^31 <p>I reworked interrupt scheduling a bit: <ul><li> <p>cp0_next_interrupt is ...
  • Interrupt subsystem rework

    2020-12-09 12:29:24
    One of the fields in those bitfields is the actual hardware interrupt number of this interrupt at its level in the interrupt hierarchy, another field is the interrupt number of the parent DSP ...
  • interrupt方法

    2019-03-24 09:55:14
    1、interrupt方法 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回。这里需要...
  • fix interrupt sleep

    2020-12-08 21:54:16
    <div><p>MySensors requires both interrupts or the first interrupt parameter to be defined. If first interrupt is INTERRUPT_NOT_DEFINE it will not work. <p>fixes #363</p><p>该提问来源于开源项目:...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 19,657
精华内容 7,862
关键字:

interrupt