线程中的核心操作

本文详细解释了Java中线程的创建(start()方法)、终止的处理方式(包括自定义线程结束逻辑和使用Thread类的interrupt()方法),以及如何确保线程的中断行为符合预期,如继续执行、立即结束和异常处理。
摘要由CSDN通过智能技术生成

1:start()

start() 真正的创建线程,(在内核中创建PCB,然后添加到链表中).
一个线程需要先通过run/lambda表达式,把先要完成的任务定义出来,start()才是真正创建线程,并开始执行.
核心:是否真的创建线程出俩,每个线程都是独立调度执行的,(相当于整个程序中多了一个执行流)
一个Thread对象,也是只能start()一次的,如果想要再创建一个线程,需要再创建另一个Thread对象.

public class Test4 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            System.out.println("hello thread");
        });
        thread.start();
        thread.start();
    }
}

上述代码会抛异常.
在这里插入图片描述

正确写法应该是创建两个Thread对象,然后再分别start();

public class Test4 {
    public static void main(String[] args) {
        Thread thread=new Thread(()->{
            System.out.println("hello thread");
        });
        Thread thread2=new Thread(()->{
            System.out.println("hello     thread2");
        });
        thread.start();
        thread2.start();
    }


}

2:中断(终止)一个线程

终止一个线程,在Java中,都只是"提醒,建议",真正要不要终止,还是由线程本体来决定.
比如:t线程正在执行,其他线程,只是提醒一下t是不是要终止了,t收到这样的提醒后,也还是根据自己的代码觉得是否要终止.

2.1:自己定义线程结束的代码

核心思路:需要让终止的线程的入口的方法尽快执行结束,(跳出循环,尽快return 都可以).

public class Demo1 {
    public  static  boolean flg=true;
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            while(flg){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        for (int i = 0; i < 3; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        flg=false;
        Thread.sleep(1000);

        System.out.println(thread.isAlive());
    }
}

在这里插入图片描述

2.1.1 存在的问题

上面的代码,有一个很明显的缺陷,那就是当main线程中执行到flg=false的时候,如果thread线程刚好处于阻塞状态,那么只能阻塞状态结束, 才能终止线程.
如果sleep()的时间比较长,那么代码的效率就比较低了.

2.2:使用Thread提供的interrupt()方法和isInterrupted()

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
        while(!Thread.currentThread().isInterrupted())    {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        });
        thread.start();
        for (int i = 0; i <3 ; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        thread.interrupt();
        
        
    }
}

currentThread():这个方法能够获取到当前线程,也就是能够获取到thread 这个引用.

isInterrupted();是线程内置的标志位,true表示这个线程要终止了,false 表示这个线程要继续执行.
thread.interrupt();通过这个方法,就相当于将标志位设置为true,同时,还可以唤醒sleep()等阻塞的方法.
有sleep()的时候,当触发Interrupt的时候,线程正在sleep,sleep被唤醒的同时,就会清除掉刚才的标志位(又改为false).
之所以要改回去,就是把控制权,交给程序员自己,当前线程是否要继续执行?,还是要立即结束?还是要等结束?让程序员自己写代码来决定.

2.2.1 继续执行

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
        while(!Thread.currentThread().isInterrupted())    {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        });
        thread.start();
        for (int i = 0; i <3 ; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        thread.interrupt();


    }
}

在这里插入图片描述

2.2.2 立即结束

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
        while(!Thread.currentThread().isInterrupted())    {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
              break;
            }
        }
        });
        thread.start();
        for (int i = 0; i <3 ; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        thread.interrupt();


    }
}

2.2.3 打印异常信息,再立即结束

public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
        while(!Thread.currentThread().isInterrupted())    {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
              break;
            }
        }
        });
        thread.start();
        for (int i = 0; i <3 ; i++) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
        thread.interrupt();


    }
}

在这里插入图片描述

2.2.1 继续执行

public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        for (int i = 0; i <10 ; i++) {
            Thread.sleep(1000);
            System.out.println("hello main");
        }
        thread.interrupt();
    }
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值