没有进路的时候,正是后劲施展最大的时候。

进行一个线程意味着在工作解决完工作之前停掉正在做的操作,也就是放弃以后的操作。进行一个线程能够用Thread.stop()办法,但最好不要用它。尽管它的确能够进行一个正在运行的线程,然而这个办法是不平安的,而且是已被废除的办法。

在java中有以下3种办法能够终止正在运行的线程:

  • 应用退出标记,使线程失常退出,也就是当run办法实现后线程终止。
  • 应用stop办法强行终止,然而不举荐这个办法,因为stop和suspend及resume一样都是过期作废的办法。
  • 应用interrupt办法中断线程。

1. 进行不了的线程

interrupt()办法的应用成果并不像for+break语句那样,马上就进行循环。调用interrupt办法是在以后线程中打了一个进行标记,并不是真的进行线程。

public class MyThread extends Thread {    public void run(){        super.run();        for(int i=0; i<500000; i++){            System.out.println("i="+(i+1));        }    }}public class Run {    public static void main(String args[]){        Thread thread = new MyThread();        thread.start();        try {            Thread.sleep(2000);            thread.interrupt();        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

输入后果:

...i=499994i=499995i=499996i=499997i=499998i=499999i=500000

2. 判断线程是否进行状态

Thread.java类中提供了两种办法:

  • this.interrupted(): 测试以后线程是否曾经中断;
  • this.isInterrupted(): 测试线程是否曾经中断;

那么这两个办法有什么图区别呢?

咱们先来看看this.interrupted()办法的解释:测试以后线程是否曾经中断,以后线程是指运行this.interrupted()办法的线程。

public class MyThread extends Thread {    public void run(){        super.run();        for(int i=0; i<500000; i++){            i++;//            System.out.println("i="+(i+1));        }    }}public class Run {    public static void main(String args[]){        Thread thread = new MyThread();        thread.start();        try {            Thread.sleep(2000);            thread.interrupt();            System.out.println("stop 1??" + thread.interrupted());            System.out.println("stop 2??" + thread.interrupted());        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

运行后果:

stop 1??falsestop 2??false

类Run.java中尽管是在thread对象上调用以下代码:thread.interrupt(), 前面又应用

System.out.println("stop 1??" + thread.interrupted());System.out.println("stop 2??" + thread.interrupted());  

来判断thread对象所代表的线程是否进行,但从控制台打印的后果来看,线程并未进行,这也证实了interrupted()办法的解释,测试以后线程是否曾经中断。这个以后线程是main,它从未中断过,所以打印的后果是两个false.

如何使main线程产生中断成果呢?

public class Run2 {    public static void main(String args[]){        Thread.currentThread().interrupt();        System.out.println("stop 1??" + Thread.interrupted());        System.out.println("stop 2??" + Thread.interrupted());        System.out.println("End");    }}    

运行成果为:

stop 1??truestop 2??falseEnd

办法interrupted()确实判断出以后线程是否是进行状态。但为什么第2个布尔值是false呢?官网帮忙文档中对interrupted办法的解释:

测试以后线程是否曾经中断。线程的中断状态由该办法革除。换句话说,如果间断两次调用该办法,则第二次调用返回false。

上面来看一下inInterrupted()办法。

public class Run3 {    public static void main(String args[]){        Thread thread = new MyThread();        thread.start();        thread.interrupt();        System.out.println("stop 1??" + thread.isInterrupted());        System.out.println("stop 2??" + thread.isInterrupted());    }}

运行后果:

stop 1??truestop 2??true

isInterrupted()并为革除状态,所以打印了两个true。

3. 能进行的线程--异样法

有了后面学习过的知识点,就能够在线程中用for语句来判断一下线程是否是进行状态,如果是进行状态,则前面的代码不再运行即可:

public class MyThread extends Thread {    public void run(){        super.run();        for(int i=0; i<500000; i++){            if(this.interrupted()) {                System.out.println("线程曾经终止, for循环不再执行");                break;            }            System.out.println("i="+(i+1));        }    }}public class Run {    public static void main(String args[]){        Thread thread = new MyThread();        thread.start();        try {            Thread.sleep(2000);            thread.interrupt();        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

运行后果:

...i=202053i=202054i=202055i=202056

线程曾经终止, for循环不再执行

下面的示例尽管进行了线程,但如果for语句上面还有语句,还是会持续运行的。看上面的例子:

public class MyThread extends Thread {    public void run(){        super.run();        for(int i=0; i<500000; i++){            if(this.interrupted()) {                System.out.println("线程曾经终止, for循环不再执行");                break;            }            System.out.println("i="+(i+1));        }        System.out.println("这是for循环里面的语句,也会被执行");    }}

应用Run.java执行的后果是:

...i=180136i=180137i=180138i=180139

线程曾经终止, for循环不再执行

这是for循环里面的语句,也会被执行

如何解决语句持续运行的问题呢?看一下更新后的代码:

public class MyThread extends Thread {    public void run(){        super.run();        try {            for(int i=0; i<500000; i++){                if(this.interrupted()) {                    System.out.println("线程曾经终止, for循环不再执行");                    throw new InterruptedException();                }                System.out.println("i="+(i+1));            }            System.out.println("这是for循环里面的语句,也会被执行");        } catch (InterruptedException e) {            System.out.println("进入MyThread.java类中的catch了。。。");            e.printStackTrace();        }    }}

应用Run.java运行的后果如下:

...i=203798i=203799i=203800线程曾经终止, for循环不再执行进入MyThread.java类中的catch了。。。java.lang.InterruptedExceptionat thread.MyThread.run(MyThread.java:13)

4. 在沉睡中进行

如果线程在sleep()状态下进行线程,会是什么成果呢?

public class MyThread extends Thread {    public void run(){        super.run();        try {            System.out.println("线程开始。。。");            Thread.sleep(200000);            System.out.println("线程完结。");        } catch (InterruptedException e) {            System.out.println("在沉睡中被进行, 进入catch, 调用isInterrupted()办法的后果是:" + this.isInterrupted());            e.printStackTrace();        }    }}

应用Run.java运行的后果是:

线程开始。。。在沉睡中被进行, 进入catch, 调用isInterrupted()办法的后果是:falsejava.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at thread.MyThread.run(MyThread.java:12)

从打印的后果来看, 如果在sleep状态下进行某一线程,会进入catch语句,并且革除进行状态值,使之变为false。

前一个试验是先sleep而后再用interrupt()进行,与之相同的操作在学习过程中也要留神:

public class MyThread extends Thread {    public void run(){        super.run();        try {            System.out.println("线程开始。。。");            for(int i=0; i<10000; i++){                System.out.println("i=" + i);            }            Thread.sleep(200000);            System.out.println("线程完结。");        } catch (InterruptedException e) {            System.out.println("先进行,再遇到sleep,进入catch异样");            e.printStackTrace();        }    }}public class Run {    public static void main(String args[]){        Thread thread = new MyThread();        thread.start();        thread.interrupt();    }}

运行后果:

i=9998i=9999先进行,再遇到sleep,进入catch异样java.lang.InterruptedException: sleep interrupted        at java.lang.Thread.sleep(Native Method)        at thread.MyThread.run(MyThread.java:15)

5. 能进行的线程---暴力进行

应用stop()办法进行线程则是十分暴力的。

public class MyThread extends Thread {    private int i = 0;    public void run(){        super.run();        try {            while (true){                System.out.println("i=" + i);                i++;                Thread.sleep(200);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }}public class Run {    public static void main(String args[]) throws InterruptedException {        Thread thread = new MyThread();        thread.start();        Thread.sleep(2000);        thread.stop();    }}

运行后果:

i=0i=1i=2i=3i=4i=5i=6i=7i=8i=9Process finished with exit code 0

6.办法stop()与java.lang.ThreadDeath异样

调用stop()办法时会抛出java.lang.ThreadDeath异样,然而通常状况下,此异样不须要显示地捕获。

public class MyThread extends Thread {    private int i = 0;    public void run(){        super.run();        try {            this.stop();        } catch (ThreadDeath e) {            System.out.println("进入异样catch");            e.printStackTrace();        }    }}public class Run {    public static void main(String args[]) throws InterruptedException {        Thread thread = new MyThread();        thread.start();    }}

stop()办法以及作废,因为如果强制让线程进行有可能使一些清感性的工作得不到实现。另外一个状况就是对锁定的对象进行理解锁,导致数据得不到同步的解决,呈现数据不统一的问题。

7. 开释锁的不良后果

应用stop()开释锁将会给数据造成不一致性的后果。如果呈现这样的状况,程序处理的数据就有可能受到毁坏,最终导致程序执行的流程谬误,肯定要特地留神:

public class SynchronizedObject {    private String name = "a";    private String password = "aa";    public synchronized void printString(String name, String password){        try {            this.name = name;            Thread.sleep(100000);            this.password = password;        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}public class MyThread extends Thread {    private SynchronizedObject synchronizedObject;    public MyThread(SynchronizedObject synchronizedObject){        this.synchronizedObject = synchronizedObject;    }    public void run(){        synchronizedObject.printString("b", "bb");    }}public class Run {    public static void main(String args[]) throws InterruptedException {        SynchronizedObject synchronizedObject = new SynchronizedObject();        Thread thread = new MyThread(synchronizedObject);        thread.start();        Thread.sleep(500);        thread.stop();        System.out.println(synchronizedObject.getName() + "  " + synchronizedObject.getPassword());    }}

输入后果:

b  aa

因为stop()办法以及在JDK中被表明为“过期/作废”的办法,显然它在性能上具备缺点,所以不倡议在程序张应用stop()办法。

8. 应用return进行线程

将办法interrupt()与return联合应用也能实现进行线程的成果:

public class MyThread extends Thread {    public void run(){        while (true){            if(this.isInterrupted()){                System.out.println("线程被进行了!");                return;            }            System.out.println("Time: " + System.currentTimeMillis());        }    }}public class Run {    public static void main(String args[]) throws InterruptedException {        Thread thread = new MyThread();        thread.start();        Thread.sleep(2000);        thread.interrupt();    }}

输入后果:

...Time: 1467072288503Time: 1467072288503Time: 1467072288503线程被进行了!

不过还是倡议应用“抛异样”的办法来实现线程的进行,因为在catch块中还能够将异样向上抛,使线程进行事件得以流传。