如何关掉一个线程

1、Thread.stop()java

此方法已通过时,不推荐了。。。安全

由于它本质上是不安全的。中止线程会致使它解锁全部已锁定的监视器。(当ThreadDeath异常在堆栈中传播时,监视器被解锁。)若是以前由这些监视器保护的对象中的任何一个处于不一致状态,则其余线程如今能够以不一致的状态查看这些对象。据称这些物体被 损坏。当线程操做受损对象时,可能致使任意行为。就比如电脑的冷关机,或者忽然断电。没有作任务的保护措施!可能会让你丢失数据。。。因此称之为不安全的!ide

public class Test {
    private int lock = 1;
    public void volatileTest() {
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                int n = 0;
                while (lock == 1){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("T1--"+ n++ +":runnable is running...");
                }
                System.out.println("T1--"+ n+":runnable is run out...");
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                int n = 0;
                while (lock == 1){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("T2--"+ n++ +":runnable is running...");
                }
                System.out.println("T2--"+ n+":runnable is run out...");
            }
        };
        Thread t1 =  new Thread(runnable1);
        Thread t2 =  new Thread(runnable2);
        t1.start();
        t2.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.stop();  //你再键入这个方法时候,IDE会提醒你这个方法如今已通过时,不推荐了!

    }
}

2、利用volatile的可见性做为开关控制器函数

详见另外一篇文章的描述:http://www.javashuo.com/article/p-gwsefwld-cx.htmlspa

3、Thread.interrupt()/isInterrupted/interrupted配合的信号。使得线程被中断抛出异常!.net

1)RUNNING状态线程—>先被阻塞成BLOCKED->再被中断—>flag=true!线程

2) RUNNING状态线程—>先被中断—>flag = true—>若再被阻塞—>flag = false!code

3)interrupted()方法先返回当前的状态flag(无论是true仍是false),同时又把标志位清除为false(无论以前是true仍是false)对象

4)isInterrupted()方法只是返回当前的flag状态,不作任务赋值处理!blog

5)interrupted和isInterrupted的最大区别在于他们的源码定义中,一个传入清除参数为true,另外一个传入为false!

 

 

 

 

其实Thread.interrupt功能意图是让阻塞BLOCKED的线程被中断掉

isInterrupted()源码,其中false表示不清楚标志位!

public boolean isInterrupted() {
    return isInterrupted(false);
}

要中断一个Java线程,可调用线程类(Thread)对象的实例方法:interrupte();然而interrupte()方法并不会当即执行中断操做;具体而言,这个方法只会给线程设置一个为true的中断标志(中断标志只是一个布尔类型的变量),而设置以后,则根据线程当前的状态进行不一样的后续操做。若是,线程的当前状态处于非阻塞状态,那么仅仅是线程的中断标志被修改成true而已;若是线程的当前状态处于阻塞状态,那么在将中断标志设置为true后,还会有以下三种状况之一的操做:

  • 若是是wait、sleep以及jion三个方法引发的阻塞,那么会将线程的中断标志从新设置为false,并抛出一个InterruptedException;
  • 若是是java.nio.channels.InterruptibleChannel进行的io操做引发的阻塞,则会对线程抛出一个ClosedByInterruptedException;(待验证)
  • 若是是轮询(java.nio.channels.Selectors)引发的线程阻塞,则当即返回,不会抛出异常。(待验证)

    若是在中断时,线程正处于非阻塞状态,则将中断标志修改成true,而在此基础上,一旦进入阻塞状态,则按照阻塞状态的状况来进行处理;例如,一个线程在运行状态中,其中断标志被设置为true,则此后,一旦线程调用了wait、jion、sleep方法中的一种,立马抛出一个InterruptedException,且中断标志被清除,从新设置为false。

若是非阻塞---标志位置为true

若是阻塞-----标志位也置为true,而后还会继续分好多状况!若是是sleep引发的阻塞方式,则标志位从新清除为false!

若是这时候调用了interrupted函数,第一次返回true以后,再将标志位置为false!!而后第二第三次都是...false!

isInterrupted【外】]=false
thread.interrupt()....中断==当前线程状态==BLOCKED
isInterrupted【外】=true
interrupted【外】=false
interrupted【外】=false
执行线程体...
interrupted【内前】=true
进入睡眠...=RUNNABLE
结束线程...
interrupted【内后】=false
isInterrupted【外】]=false
thread.interrupt()....中断==当前线程状态==RUNNABLE
isInterrupted【外】=true
interrupted【外】=false
interrupted【外】=false
interrupted【内前】执行时,还没被打断=true
执行线程体...
进入睡眠...=RUNNABLE
结束线程...
interrupted【内后】=false
相关文章
相关标签/搜索