线程知多少

一、中断线程

让线程中断,就是让内核里的PCB销毁,线程结束的关键,就是让线程对应的入口方法,执行完毕。入口方法是:继承Thread重写run,实现Runnable重写run,lambda,只要run执行完,线程就随之结束,实际情况下,线程不能这么快执行完run方法,如果run里面带的是一个死循环,此时这个线程就会一直运行,直到整个进程结束,实际开发中,并不希望run就是一个死循环,而是希望能够控制这个线程,能够按照我们需要随时结束,为了达到这个效果,就有一些办法:

1、用boolean变量作为循环结束的标记

public class ThreadDemo10 {
    private static boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (flag){
                    System.out.println("线程运行中.....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程结束!");

            }
        };
        t.start();

        //主循环中也等待3秒
        Thread.sleep(3000);

        //3秒中之后,就把flag改成false
        flag = false;

    }
}

2、使用标准库里内置的标记

获取线程内置的标记位:线程的isInterrupted()判定当前线程是不是要应该结束循环

修改线程内置的标记位:Thread.interrupt()来修改这个标记位

public class ThreadDemo11 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()){
                    System.out.println("线程运行中....");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();

        //在主线程中,通过t.interrupt()方法来设置这个标记位
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //这个操作就是把Thread.currentThread().isInterrupted()给设成true
        t.interrupt();
    }
}

 

二、线程等待 

 

public class ThreadDemo12 {
    public static void main(String[] args) {
        //创建两个线程t1
        Thread t1 = new Thread(){
            @Override
            public void run() {
                int count = 0;
                while (count < 5){
                    count++;
                    System.out.println("线程运行中");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程运行结束");
            }
        };
        t1.start();

        try {
            //此处join就会阻塞等待
            System.out.println("join执行开始");
            t1.join();
            System.out.println("join执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

 

public class ThreadDemo13 {
    public static void main(String[] args) {
        Thread t = new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getId());
                System.out.println(this.getId());
            }
        };
        //在这个代码中,得到的就是t这个引用,也就相当于是在run中直接使用this
        t.start();
    }
}

获取当前线程引用 

 

 

 

线程的状态 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值