终止线程的集中方式

来源:互联网 发布:淘宝如何清除缓存 编辑:程序博客网 时间:2024/04/30 09:49

1 . 用标准输入来阻止当前线程

package com.qf.demo3;import java.io.IOException;import java.io.InputStream;public class Test6 {    public static void main(String[] args) {        Thrid thrid = new Thrid();        thrid.start();        // 用标准输入 是为了拖延时间, 让  线程能够执行一会        InputStream is = System.in;        try {            is.read();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        // 通过 将表这位设置为false 来停止线程        thrid.flag = false;    }}class Thrid extends Thread{    boolean flag  = true;    @Override    public void run() {        while(flag){            try {                System.out.println("临睡之前");                Thread.sleep(2000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println("睡醒了");        }    }}

2 . stop可以停止线程,但是可能有执行不到的代码,即是可能while中的代码就执行到输出”临睡之前”,”睡醒了”还没执行就停止线程了.(这种方法不推荐)

package com.qf.demo3;import java.io.IOException;import java.io.InputStream;public class Test7 {    public static void main(String[] args) {        Forth forth = new Forth();        forth.start();        InputStream is = System.in;        try {            is.read();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        // 可以停止线程, 有可能会有 执行不到的代码, 所以不推荐使用这种停止线程的方式        forth.stop();    }}class Forth extends Thread{    boolean flag  = true;    @Override    public void run() {        while(flag){            try {                System.out.println("临睡之前");                Thread.sleep(2000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println("睡醒了");        }    }}

3 . interrupt()方法, 通过抛出InterruptedException 来 打断线程的 , 可以保证run方法中的内容会执行完.
(转)Thread.interrupt()方法不会中断一个正在运行的线程。它的作用是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

package com.qf.demo3;import java.io.IOException;import java.io.InputStream;public class Test8 {    public static void main(String[] args) {         Fifth fifth = new Fifth();         fifth.start();         InputStream is = System.in;         try {            is.read();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }         // 通过抛出InterruptedException 来 打断线程的 , 可以保证run方法中的内容 执行完         fifth.interrupt();    }}class Fifth extends Thread{    @Override    public void run() {        System.out.println("进入睡眠");        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            System.out.println("被打醒的");        }        System.out.println("睡到自然醒");    }}

总结上面代码:当线程处于sleep()阻塞状态时,主线程会占用cpu,现在输入数据,之后执行到fifth.interrupt(),抛出中断异常,catch捕获异常,执行System.out.println(“被打醒的”);再执行System.out.println(“睡到自然醒”);

原创粉丝点击