java多线程:中断

来源:互联网 发布:国外对网络直播的研究 编辑:程序博客网 时间:2024/06/07 03:27

一、java中终止线程主要有三种方法:
①线程正常退出,即run()方法执行完毕了
②使用Thread类中的stop()(已过期不推荐使用)方法强行终止线程。
③使用中断机制
t.stop()调用时,终止线程,会导致该线程所持有的锁被强制释放,从而被其他线程所持有,因此有可能导致与预期结果不一致。下面使用中断信号量中断非阻塞状态的线程中:

public class TestStopThread {    public static void main(String[] args) throws InterruptedException {        StopThread st = new StopThread();        st.setName("线程st");        st.start();        Thread.sleep(3000);        st.stopFlag();        Thread.sleep(1000);        System.out.println(st.getState());    }}class StopThread extends Thread {    // 此变量必须加上volatile    private volatile boolean stop = false;    @Override    public void run() {        // 判断线程体是否运行        while (!stop) {            System.out.println("线程StopThread正在运行");            long time = System.currentTimeMillis();            /*             * 使用while循环模拟 sleep 方法,这里不要使用sleep,否则在阻塞时会抛             * InterruptedException异常而退出循环,这样while检测stop条件就不会执行,             * 失去了意义。             */            while ((System.currentTimeMillis() - time < 1000)) {}        }        System.out.println("线程StopThread正在结束");    }    // 线程终止    public void stopFlag() {        stop = true;    }}

二、java线程中断机制
下面看看Thread类里的三个方法:
1. public static boolean interrupted():检测当前线程是否已经中断。线程的中断状态由该方法清除。如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。
2. public boolean isInterrupted():测试线程是否已经中断。线程的中断状态不受该方法的影响。
3. public void interrupt(): 中断线程。
interrupt()只是改变中断状态而已. interrupt()不会终止一个正在运行的线程。

public class TestInterrupt1 {    public static void main(String[] args) throws InterruptedException {        Thread t = new MyThread();        t.start();        t.interrupt();        System.out.println("调用线程的interrupt()方法");        System.out.println("线程的中断状态:" +t.isInterrupted());    }    static class MyThread extends Thread {        public void run() {            long time = System.currentTimeMillis();            System.out.println("线程正在运行");            /*             * 使用while循环模拟 sleep 方法,这里不要使用sleep,否则在阻塞时会抛             * InterruptedException异常而退出循环。             */            while ((System.currentTimeMillis() - time < 1000)) {}            System.out.println("线程的中断状态:" + Thread.interrupted());            System.out.println("线程的中断状态被清除:" +isInterrupted());            while ((System.currentTimeMillis() - time < 5000)) {}            System.out.println("线程运行完成");        }    }}

正常输出:

调用线程的interrupt()方法线程正在运行线程的中断状态:true线程的中断状态:true线程的中断状态被清除:false线程运行完成

实际上当调用interrupt()方法的时候,只是设置了要中断线程的中断状态,而此时被中断的线程的可以通过isInterrupted()或者是Thread.interrupted()方法判断当前线程的中断状态是否标志为中断。
调用线程的wait(), wait(long)或wait(long, int)会让它进入等待(阻塞)状态,或者调用线程的join(), join(long), join(long, int), sleep(long), sleep(long, int)也会让它进入阻塞状态。若线程在阻塞状态时,调用了它的interrupt()方法,那么它的“中断状态”会被清除并且会收到一个InterruptedException异常。例如,线程通过wait()进入阻塞状态,此时通过interrupt()中断该线程;调用interrupt()会立即将线程的中断标记设为“true”,但是由于线程处于阻塞状态,所以该“中断标记”会立即被清除为“false”,同时,会产生一个InterruptedException的异常。

public class InterruptTest extends Thread{    public static void main(String[] args) throws InterruptedException {        InterruptTest t=new InterruptTest();        t.start();        Thread.sleep(1000);        t.interrupt();    }    public void run(){        while(!Thread.interrupted()){            System.out.println("Thread is running.....");            try {                Thread.sleep(5000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

正常运行结果:
运行结果
上面只是一种情况,如果线程被调用了interrupt(),那么如果此次该线程并不在wait(),sleep(),join()时,下次执行wait(),sleep(),join()时,一样会抛出InterruptedException,当然抛出后该线程的中断状态也回被系统复位。我们可以手动的使用Thread.interrupted()来使当前线程的中断状态系统复位(即清除中断状态),其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
下面看中断的源码:

    /**     * 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() {        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();    }    /**     * 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 static boolean interrupted() {        return currentThread().isInterrupted(true);    }    /**     * Tests whether this thread has been interrupted.  The <i>interrupted     * status</i> of the thread is unaffected by this method.     *     * <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 this thread has been interrupted;     *          <code>false</code> otherwise.     * @see     #interrupted()     * @revised 6.0     */    public boolean isInterrupted() {        return isInterrupted(false);    }    /**     * 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);

中断:如果线程在调用Object类的wait()、wait(long)或wait(long, int)方法,或者该类的 join() 、join(long) 、join(long, int) 、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
如果该线程在可中断的通道(java.nio.channels.InterruptibleChannel)上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个 ClosedByInterruptException。
如果该线程在一个 Selector (java.nio.channels.Selector) 中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的 wakeup 方法一样。
如果以前的条件都没有保存,则该线程的中断状态将被设置。
中断一个不处于活动状态的线程不需要任何作用。
检测中断:如何检测中断决定于线程所做的事情。
如果线程调用可以抛出InterruptException的方法,则捕获InterruptException,然后在catch块中处理(通常是退出run方法以中断线程)
如果调用其它方法,则可以在空闲时检查Thread.interrupted以判断是否收到中断信号,确认收到中断信号后进行处理。可以抛出一个InterruptException从而和前一种处理方法保持一致
中断状态:线程的中断机制是使用中断状态这一内部标志实现的。中断状态在调用线程的interrupt()方法时被设置(参考上面的interrupt方法说明)。
可以发现,isInterrupted被声明为native方法,取决于JVM底层的实现。调用线程的interrupt方法,并不能立即引发中断,只是设置了JVM内部的中断标记。因此,通过检查中断标记,应用程序可以做一些特殊操作,也可以完全忽略中断。
实际上Thread.interrupt()方法实际上通过某种方式通知线程,并不会直接中止该线程。具体做什么事情由写代码的人决定,通常我们会中止该线程。
三、一些不会抛出 InterruptedException 的线程阻塞操作
对于某些线程阻塞操作,JVM并不会自动抛出InterruptedException异常。例如,某些I/O操作和内部锁操作。对于这类操作,可以用其他方式模拟中断:
1)java.io中的异步socket I/O
读写socket的时候,InputStream和OutputStream的read和write方法会阻塞等待,但不会响应java中断。不过,调用Socket的close方法后,被阻塞线程会抛出SocketException异常。
2)利用Selector实现的异步I/O
如果线程被阻塞于Selector.select(在java.nio.channels中),调用wakeup方法会引起ClosedSelectorException异常。
3)锁获取
如果线程在等待获取一个内部锁,我们将无法中断它。但是,利用Lock类的lockInterruptibly方法,我们可以在等待锁的同时,提供中断能力。

参考资料:

Thread的中断机制(interrupt)
[改善Java代码]不使用stop方法停止线程
JAVA线程的interrupt
Java中断的本质与分析

0 0