线程高级 - 优先级,线程休眠,守护线程

  • 线程优先级,理论上优先级按照从高到低顺序运行,但基本只存在理论上。实际运行关系不大。
public class priorityDeni {
    
    
    public static void main(String[] args) {
    
    
        Thread min = new Thread(){
    
    
            @Override
            public void run() {
    
    
                for (int i = 0;i<1000;i++){
    
    
                    System.out.println("min");
                }
            }
        };

        Thread max = new Thread(){
    
    
            @Override
            public void run() {
    
    
                for (int i = 0;i<1000;i++){
    
    
                    System.out.println("max");
                }
            }
        };

        Thread nor = new Thread(){
    
    
            @Override
            public void run() {
    
    
                for (int i = 0;i<1000;i++){
    
    
                    System.out.println("nor");
                }
            }
        };

        min.setPriority(Thread.MIN_PRIORITY);
        max.setPriority(Thread.MAX_PRIORITY);

        min.start();
        max.start();
        nor.start();
    }
}
  • 线程休眠,用户输入一个数字,然后从这个数字倒计时。
public class sleepDemo {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("程序开始");

        try {
    
    
            Scanner src = new Scanner(System.in);
            int num = src.nextInt();
            while (num>=0){
    
    
                System.out.println(num);
                Thread.sleep(1000);
                num--;
            }

        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }

        System.out.println("程序结束");
    }
}

线程休眠实例2,王子亲公主的故事。线程休眠过程中可以使用interrupt打断线程。

public class sleepDemo2 {
    
    
    public static void main(String[] args) {
    
    
        Thread princess = new Thread(){
    
    
            @Override
            public void run() {
    
    
                System.out.println("公主被诅咒,一直睡着");
                try {
    
    
                    Thread.sleep(999999999);
                } catch (InterruptedException e) {
    
    
                    System.out.println("公主醒过来了");
                }
                System.out.println("公主和王子过上了幸福额生活");
            }
        };

        Thread prince = new Thread(){
    
    
            @Override
            public void run() {
    
    
                System.out.println("开始亲公主");
                for (int i = 0;i<5;i++){
    
    
                    System.out.println("么么哒");
                    try {
    
    
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
                System.out.println("完事");
                princess.interrupt();
            }
        };
        prince.start();
        princess.start();
    }
}
  • 设置守护线程
public class DaemonThreadDemo {
    
    
    public static void main(String[] args) {
    
    
        Thread t= new Thread(){
    
    
            @Override
            public void run() {
    
    
                for (int i = 0;i<5;i++){
    
    
                    System.out.println("Hello world");
                    try {
    
    
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
                System.out.println("完毕");
            }
        };

        Thread gc = new Thread(){
    
    
            public void run(){
    
    
                while (true){
    
    
                    System.out.println("GC开始回收");
                    try {
    
    
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
        // 守护线程需要先设置,在运行。否则报错
        gc.setDaemon(true);
        gc.start();
    }
}

守护线程又称为后天线程,默认情况下每条线程都是普通线程
只有调用了setDaemon方法后,才能将一个线程设置为守护线程
普通线程必须执行完任务序列后才能结束
后台线程除了正常线程的结束方式外,在设置它为后台线程的线程结束时这个线程也会结束。

Guess you like

Origin blog.csdn.net/sinat_33940108/article/details/120989065