Java之join与中断线程

版权声明:LemonSnm https://blog.csdn.net/LemonSnm/article/details/90081776

Jion:

public final void join():

等待这个进程死亡 ,也就是说让调用join的线程先执行完毕,再执行其他线程

代码示例:

package com.lemon;

/**
 * join方法:
 * 加入线程,让调用的线程先执行完毕
 */
public class ThreadDemo2 {
    public static void main(String[] args) {

        MyRunnable2 mr2 = new MyRunnable2();
        Thread t = new Thread(mr2);
        t.start();

        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "--" + i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i == 5 ){
                try {
                    t.join();    //让t线程执行完毕
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class MyRunnable2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "--" + i);
            try {
                Thread.sleep(300);     //休眠300毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}


 中断线程:

中断这个线程 

中断线程的两种方式:

在上面代码的基础上做修改:

第一种方式: 三步走:

①在外部添加中断标记

②在需要中断的线程中 判断是否有终端标记 

③在sleep休眠的catch中继续添加当前线程的中断标记

中断前需要的数据处理,在第二步判断中处理 

第一步:添加中断标记interrupt()

interrupt() 给当前线程一个中断标记

只加这一步,应为之前调用了线程休眠sleep,当线程休眠时,调用中断线程,标记会被清除,无法中断

            if(i == 5 ){
               /* try {
                    t.join();    //让t线程执行完毕
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
               t.interrupt();    //中断线程
            }

第二步:判断是否有标记

Thread.interrupted()​​​​​​   测试中断状态,此方法会把中断状态清除

因为sleep抛出异常后。清除了中断状态,无法中断

class MyRunnable2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if(Thread.interrupted()) { //测试中断状态,此方法会把中断状态清除
                   break;      //退出循环
            }
            System.out.println(Thread.currentThread().getName() + "--" + i);
            try {
                Thread.sleep(300);     //休眠300毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

第三步:重新加入当前线程的标记

在sleep抛出异常的catch中 重新添加中断状态

class MyRunnable2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if(Thread.interrupted()) { //测试中断状态,此方法会把中断状态清除
                   break;      //退出循环
            }
            System.out.println(Thread.currentThread().getName() + "--" + i);
            try {
                Thread.sleep(300);     //休眠300毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt(); //添加中断状态 标记
            }
        }

    }
}

第二种方式:自定义标记中断线程(推荐使用)

更加简洁,方便

代码示例:

public class ThreadDemo2 {
    public static void main(String[] args) {

        MyRunnable3 mr3 = new MyRunnable3();
        Thread t3 = new Thread(mr3);

        t3.start();//启动线程

        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "--" + i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i == 5 ){
               /* try {
                    t.join();    //让t线程执行完毕
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
//               t.interrupt();    //中断线程 标记
               mr3.flag =false;    //自定义标记
            }
        }
    }
}

class MyRunnable3 implements Runnable{
   public boolean flag = true;  //自定义标记

    public MyRunnable3() {
        flag = true;
    }

    @Override
    public void run() {
        int i=0;
        while(flag){        //自定义标记
            System.out.println(Thread.currentThread().getName() + "==" + (i++));

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }
}

猜你喜欢

转载自blog.csdn.net/LemonSnm/article/details/90081776