线程专区---第3集中断线程的5种方式

1 通过改变 中断状态 来 停止正在线程中根据 中断状态来执行的while循环

package mythread.第3集中断线程的5种方式;

/**
 * 通过改变 中断状态  来 停止正在线程中根据 中断状态来执行的while循环
 */
public class Main1 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        t.start();
        Thread.sleep(1); // 暂停1毫秒
        t.interrupt(); // 中断t线程
        t.join(); // 等待t线程结束
        System.out.println("end");
    }
}

class MyThread extends Thread {
    public void run() {
        int n = 0;
        while (! isInterrupted()) {
            n ++;
            System.out.println(n + " hello!");
        }
    }
}

2 通过共享变量的方式结束while循环

package mythread.第3集中断线程的5种方式;

/**
 * 通过共享变量的方式结束while循环
 */
public class Main2 {
    public static void main(String[] args)  throws InterruptedException {
        HelloThread t = new HelloThread();
        t.start();
        Thread.sleep(1);
        t.running = false; // 标志位置为false
    }
}

class HelloThread extends Thread {
    public volatile boolean running = true;//这里 必须是volatile,不然变量在主内存和本地内存可能存在值不一样的情况
    public void run() {
        int n = 0;
        while (running) {
            n ++;
            System.out.println(n + " hello!");
        }
        System.out.println("end!");
    }
}

3 通过在主线程对处于等待状态的线程1执行interrupt方法,而抛InterruptedException的方式结束线程1

package mythread.第3集中断线程的5种方式;

/**
 *
 * 通过在主线程对处于等待状态的线程1执行interrupt方法,而抛InterruptedException的方式结束线程1
 *
 *  1 主线程中创建了一个线程1
 *  2 线程1中创建了一个线程2
 *  3 线程1调用了线程2的join方法而进入了等待状态
 *  4 主线程对线程1调用了 interrupt 方法之后,此时的线程1还在因为线程2而处于等待状态,所以线程1调用的thread2.join会进入InterruptedException异常而catch
 */
public class Main3 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("我是主线程,我开始执行thread1了");
        Thread thread1 = new MyThread1();
        thread1.start();
        System.out.println("我是主线程,我即将休息1秒钟");
        Thread.sleep(1000);
        System.out.println("我是主线程,我已经休息了1秒钟了");
        System.out.println("我是主线程,我要执行thread1.interrupt()方法,中断thread1的线程状态");
        thread1.interrupt(); // 中断t线程
        System.out.println("我是主线程,我要执行thread1.join()方法,等待thread1 执行完毕");
        thread1.join(); // 等待thread1线程结束
        System.out.println("我是主线程,我已经跑完了");
    }
}

class MyThread1 extends Thread {
    public void run() {
        System.out.println("我是线程1,我开始执行");
        System.out.println("我是线程1,我要开始启动thread2了");
        Thread thread2 = new MyThread2();
        thread2.start(); // 启动thread2线程
        try {
            System.out.println("我是线程1,因为调用了thread2.join方法,我进入了堵塞状态");
            thread2.join(); // 等待myThread2线程结束
            System.out.println("我是线程1,因为调用了thread2.join方法,我进入了堵塞状态");
        } catch (InterruptedException e) {
            System.out.println("我是MyThread1,我在执行thread2.join()的过程中报错,现在正在执行catch方法");
        }
        thread2.interrupt();
    }
}

class MyThread2 extends Thread {
    public void run() {
        System.out.println("我是线程2,我开始执行");
        int n = 0;
        while (!isInterrupted()) {
            n++;
            System.out.println("我是线程2,我正在执行while循环");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}

4 如果子线程处于wait状态,那么主线程对子线程执行interrupt时,子线程会报出异常,此时如果抛出,则子线程结束

package mythread.第3集中断线程的5种方式;

import lombok.SneakyThrows;

/**
 * 如果子线程处于wait状态,那么主线程对子线程执行interrupt时,子线程会报出异常,此时如果抛出,则子线程结束
 */
public class Main4 {
    public static void main(String[] args) throws InterruptedException {
        Main4_thread main4_thread = new Main4_thread();
        main4_thread.start();
        Thread.sleep(1000);
        main4_thread.interrupt();
    }

}


class Main4_thread extends Thread {
    public volatile boolean running = true;// volatile 保证主线程和子线程的变量实时同步

    @SneakyThrows// 作用抛出异常,不需要写 try catch 或者 thow
    public void run() {
        while (true) {
            Thread.sleep(100);
            System.out.println(1);
        }
    }
}

5 如果子线程处于wait状态,那么主线程对子线程执行interrupt时,子线程会报出异常,此时如果抛出,则子线程结束

package mythread.第3集中断线程的5种方式;

import lombok.SneakyThrows;

/**
 * 如果子线程处于wait状态,那么主线程对子线程执行interrupt时,子线程会报出异常,此时如果抛出,则子线程结束
 */
public class Main5 {
    public static void main(String[] args) throws InterruptedException {

        Main5_thread main5_thread = new Main5_thread();
        main5_thread.start();
        main5_thread.interrupt();
    }

}


class Main5_thread extends Thread {
    public volatile boolean running = true;// volatile 保证主线程和子线程的变量实时同步
    @SneakyThrows// 作用抛出异常,不需要写 try catch 或者 thow
    public void run() {
            System.out.println(111);
            this.wait();
            System.out.println(1);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值