【狂神说Java】线程优先级,守护线程

时间:2021-09-24 00:36:04   收藏:0   阅读:32

线程优先级

public class TestPriority {
    public static void main(String[] args) {
        // main的线程优先级
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
        MyPriority myPriority = new MyPriority();
        Thread t = new Thread(myPriority,"a");
        Thread t2 = new Thread(myPriority,"b");
        t.start();
        t2.setPriority(10);// 设置优先级
        t2.start();
    }
}

class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
    }
}

守护(daemon)线程

public class TestDaemon {
    public static void main(String[] args) {
        Lucky lucky = new Lucky();
        Youlucky you = new Youlucky();

        Thread godt = new Thread(lucky);
        godt.setDaemon(true);// 默认是false,有true代表是守护进程
        godt.start(); // 守护线程

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

class Lucky implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("运气一直在");
        }
    }
}

class Youlucky implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("运气好");
        }
        System.out.println("================================See You!=======================================");
    }
}

原文:https://www.cnblogs.com/jie7/p/15306025.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!