多线程 - 线程状态

线程状态

创建,就绪,运行,阻塞,死亡。

线程五大状态

在这里插入图片描述

线程结束 stop()

  • 不推荐使用JDK提空的 stop,destroy 方法来结束线程。
  • 推荐线程自己停下来
  • 建议使用一个标志位进行终止标量,如当 flag = false 时结束线程。
public class ThreadTest01 implements Runnable {
    //1 定义线程结束的标识
    private boolean flag = true;

    @Override
    public void run() {
        //2 线程使用该标识
        while (flag){
            System.out.println("方法体");
        }
    }

    //3 结束线程的方法
    public void threadStop( ) {
        this.flag = false;
    }


    public static void main(String[] args) {
        ThreadTest01 threadTest01 = new ThreadTest01();
        new Thread(threadTest01).start();
        for (int i = 0; i < 1000; i++) {
            if (i >= 900){
                threadTest01.threadStop();
            }
            System.out.println("main : " + i);
        }
    }
}

线程休眠 sleep()

  • 线程休眠
    1. sleep(时间) 指定线程休眠多长时间(毫秒);
    2. sleep 存在异常,需要抛出。
    3. sleep 时间到了后线程进入就绪状态。
    4. sleep 可以模拟网络延迟 ,倒计时等。
    5. 每一个队形斗鱼一个锁,sleep 不会释放锁。
// 模拟网络延时
public class ThreadTest02   {
    
    public static void main(String[] args) throws InterruptedException {
        toTimeDown();
    }

    public static void toTimeDown() throws InterruptedException {
        int num = 10;
        while (true){
            Thread.sleep(1000);
            System.out.println(num--);
            if (num<=0){
                break;
            }
        }
    }
}

线程礼让 yield()

  • 礼让线程,让当前的正在执行的线程暂停,但不会阻塞。
  • 将线程从运行状态转为就绪状态
  • 让 CPU 重新调度,礼让不一定会成功。由cpu决定
public class ThreadTest03 {

    public static void main(String[] args) throws InterruptedException {
        myYield myYield = new myYield();
        new Thread(myYield,"a").start();
        new Thread(myYield,"b").start();
    }
}
class myYield implements  Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
        // 线程礼让
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程结束");
    }
}

礼让就是 重新 竞争线程。

线程插队 join()

  • join 合并线程,待此线程执行完成后,再执行其他线程。
  • 也就是说,阻塞其他的所有线程,所以其实应该是插队线程
  • join 的最重要的部分,就是插队可以保证,这个线程自己一定会先执行完,这是在很多地方需要的逻辑。
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (int i=0; i<100; i++){
                System.out.println("myThread is joining" + i);
            }
        });

        t.start();

        for (int i=0; i<100; i++){
            if (i == 50){
                t.join();//插队,此时主线程不能执行,而要执行 t
            }
            System.out.println("main is running" + i);
        }
    }
}

线程等待 wait()

注意事项

  1. 当前线程调用共享对象的wait()方法时,当前线程只会释放当前共享对象的锁,当前线程持有的其他共享对象的监视器锁并不会被释放,就会造成死锁
  2. 可以传递int参数里决定等待几秒
  3. 不会释放锁
    private static  Object Lipstick = new Object();
    private static  Object Mirror = new Object();

    public static class Lipstick implements Runnable {

        @Override
        public void run() {

            try{

                // 获取Lipstick的共享资源监视器锁
                synchronized (Lipstick) {
                    System.out.println("获取线程A中Lipstick的锁");
                    // 获取Mirror的共享资源的监视器锁
                    synchronized (Mirror) {
                        System.out.println("获取线程A中Mirror的锁");

                        // 线程A阻塞,并释放获取到的Lipstick的锁
                        System.out.println("将线程A挂起,并释放Lipstick的锁");
                        Lipstick.wait();
                    }
                }
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Mirror implements Runnable {

        @Override
        public void run() {

            try{
            	// 休眠1s
                Thread.sleep(1000);

                // 获取Lipstick的共享资源监视器锁
                synchronized (Lipstick) {
                    System.out.println("获取线程B中Lipstick的锁");

                    System.out.println("获取线程B中Mirror的锁");

                    // 获取Mirror的共享资源的监视器锁
                    synchronized (Mirror) {
                        System.out.println("获取线程B中Mirror的锁...");

                        // 线程B阻塞,并释放获取到的Lipstick的锁
                        System.out.println("获取线程B中Mirror的锁...");
                        Lipstick.wait();
                    }
                }
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

       Thread a = new Thread(new Lipstick());
       Thread b = new Thread(new Mirror());

       a.start();
       b.start();

       a.join();
       b.join();
    }

线程唤醒 notifyAll()

唤醒所有等待的线程。

一般和wait 搭配使用

  Father father = new Father();
  Thread thread = new Thread(father);
  thread.notifyAll();

线程的优先级

Java中线程优先级用1~10来表示,分为三个级别:

  • 低优先级:1~4,其中类变量Thread.MIN_PRORITY最低,数值为1;
  • 默认优先级:如果一个线程没有指定优先级,默认优先级为5,由类变量Thread.NORM_PRORITY表示;
  • 高优先级:6~10,类变量Thread.MAX_PRORITY最高,数值为10。

最终执行的还是由CPU来决定,只是优先级高的先执行的几率大了。

优先级低只是意味着获取的调度的概率低,并不是优先级低的就不会被提前调用,这都要看CPU的调度。

//测试线程的优先级
public class TestPriority extends Thread {
    public static void main(String[] args) {
        //主线程优先级
        System.out.println(Thread.currentThread().getName()+
                "--》"+Thread.currentThread().getPriority());
        MyPriorty myPriorty = new MyPriorty();
        Thread t1 = new Thread(myPriorty);
        Thread t2 = new Thread(myPriorty);
        Thread t3 = new Thread(myPriorty);
        Thread t4 = new Thread(myPriorty);
        Thread t5 = new Thread(myPriorty);
        //默认优先级
        t1.start();
        //最小的 
        t2.setPriority(Thread.MIN_PRIORITY);
        t2.start();
        //最大的
        t3.setPriority(Thread.MAX_PRIORITY);
        t3.start();
		//小于0或者大于10 会出现异常
        t4.setPriority(-1);
        t4.start();

        t5.setPriority(7);
        t5.start();
    }
}

class MyPriorty implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+
                "--》"+Thread.currentThread().getPriority());
    }
}

守护线程

  • 线程分 用户线程守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
  • 如,后台记录日志,监控内存,GC等。。
//测试守护线程
//父亲保护你
public class TestDemo01 {
    public static void main(String[] args) {
        Father father = new Father();
        You you = new You();

        Thread thread = new Thread(father);
        //设置为守护线程,不会停止 ,默认为false 用户 线程
        thread.setDaemon(true);
        thread.start();

        new Thread(you).start();//用户线程
    }

}

//父亲
class  Father implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("父亲保护着你");
        }
    }
}

class  You implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("每天开心的活着");
        }
        System.out.println("GoodBye World!");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值