【多线程】线程状态、优先级、守护线程

线程五大状态

public class TestThread11 {
   public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
           for(int i=0;i<5;i++){
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
            System.out.println("");
        });
        //观察线程状态
        Thread.State state=thread.getState();
        System.out.println(state);

        //观察线程其中后
        thread.start();
        state=thread.getState();
        System.out.println(state);

        while (state != Thread.State.TERMINATED){//只要线程不终止,就一直输出状态
            Thread.sleep(100);
            state=thread.getState();//更新线程
            System.out.println(state);//输出状态

        }
    }
}

已经死亡的线程,不可再次启动

线程优先级

1,java提供一个线程调度器来监控程序中,启动后,进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行

2,线程的优先级用数字来表示,范围1-10

        Thread.MIN_PRIORITY = 1

        Thread.MAX_PRIORITY = 10

        Thread.NORM_PRIORITY = 5

3,使用以下方法改变或者获取线程优先级

        getPriority()

        setPriority(int xxx)

4,mian方法是主线程,优先级不可改,默认5

5,先设置优先级再启动

6,优先级高不一定先执行(优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用,这都是看CPU的调度)(也可能调度到优先级低情况的叫性能倒置):如下图测试结果

7,优先级不在范围内会报错。如下图实例

源码如下 

public class TestThread12Priority {
    public static void main(String[] args) {
        //先打印主线程的优先级看看,主线程优先级不可改,默认5
        System.out.println(Thread.currentThread().getName()+"======="+Thread.currentThread().getPriority());

        MyPriority myPriority=new MyPriority();

       /* Thread thread1=new Thread(myPriority);
        thread1.start();

        //先设置优先级,再启动线程
        Thread thread2=new Thread(myPriority);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread2.start();

        Thread thread3=new Thread(myPriority);
        thread3.setPriority(4);
        thread3.start();

        Thread thread4=new Thread(myPriority);
        thread4.setPriority(Thread.MAX_PRIORITY);
        thread4.start();*/

        //优先级低于1
        Thread thread5=new Thread(myPriority);
        thread5.setPriority(-1);
        thread5.start();

        //优先级高于10
        Thread thread6=new Thread(myPriority);
        thread6.setPriority(11);
        thread6.start();


    }


}

class MyPriority implements Runnable{

    @Override
    public void run() {
        //打印当前线程优先级
        System.out.println(Thread.currentThread().getName()+"======="+Thread.currentThread().getPriority());
    }

守护线程

1,线程分为用户线程和守护线程

2,虚拟机必须确保用户线程执行完毕(如:main)

3,虚拟机不用等待守护线程执行完毕(如:后台记录操作日志,监控内存,垃圾回收GC等,用户线程执行完毕,就会停止)

/**
 * 守护线程,上帝守护人类
 */
public class TestThread13Daemon {
    public static void main(String[] args) {
        Thread godthread = new Thread(new GodThread());
        Thread youThread = new Thread(new YouThread());
        godthread.setDaemon(true);//设置上帝线程是守护线程,默认是false用户线程

        godthread.start();
        youThread.start();
    }
}

class YouThread implements Runnable{
    @Override
    public void run() {
        for(int i=1;i<=100;i++){
            System.out.println("活了第"+i+"年");
        }
    }
}
class GodThread implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("God守护");
        }
    }
}

多线程15:观测线程状态_哔哩哔哩_bilibili

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值