8.启动一个线程 / 中断一个线程 / 等待一个线程

启动一个线程

调用start()方法,才能正式启动一个线程

中断一个线程

中断就是让一个线程结束,结束可能有两种情况:

1.已经把任务执行完了

以下代码为例:

public class ThreadDemo7 {
    private static boolean isQuit = false;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (!isQuit){
                    System.out.println("正在转载");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("转载被终止");
            }
        };
        t.start();

        Thread.sleep(5000);
        System.out.println("需要赶快终止交易!!");
        isQuit = true;
    }
}

在这里插入图片描述
此时的结束方式是比较温和的,如果此时执行到sleep时,已经sleep了100ms了此时isQuit被设置成了true,不会立刻结束,而是把剩下的400ms执行完了在结束。

2.任务执行了一般,被强制结束了,可以调用线程的interrupt方法来实现

t.interrupt(); 可以给该线程触发一个异常


public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                //此处直接使用线程内部的标志位
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println("正在转载");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
                System.out.println("终止转载");
            }
        };
        t.start();

        Thread.sleep(5000);
        System.out.println("赶快终止转载!!!!");
        t.interrupt();
    }
}

  1. Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志,返回结果会改变。
  2. Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志,其中Thread.currentThread().相当于this

在这里插入图片描述

等待一个线程

线程之间是并发执行的,如果现在创建一个新线程,,那么这时先打印新线程还是主线程是无法预知的。,这是抢占式执行的重要特点

虽然没法控制哪个线程先跑,但是可以控制让哪个线程先结束,哪个线程后结束

join方法执行时就会造成线程阻塞,一直阻塞到对应线程执行结束之后,才会继续执行,其存在的意义就是为了控制线程结束的先后顺序


public class ThreadDemo11 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++){
                    System.out.println("线程一");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++){
                    System.out.println("线程二");
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        
        t1.start();
        t1.join();//当执行这里的时候,线程就阻塞了,一直到t1结束,才会往下执行
        t2.start();
        t2.join();
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值