终止线程的集中方式

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(“睡到自然醒”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值