17.Priority优先级

/**
 * 优先级
 */
public class PriorityDemo {
    public static class HightPriority extends Thread{
        static int count = 0;

        @Override
        public void run() {
            while (true){
                synchronized (PriorityDemo.class){
                    count++;
                    if (count>100000000){
                        System.out.println("HightPriority is complete");
                        break;
                    }
                }
            }
        }
    }
    public static class LowPriority extends Thread{
        static int count = 0;

        @Override
        public void run() {
            while (true){
                synchronized (PriorityDemo.class){
                    count++;
                    if (count>100000000){
                        System.out.println("LowPriority is complete");
                        break;
                    }
                }
            }
        }
    }
    public static void main(String[] args){
        HightPriority hightPriority = new HightPriority();
        LowPriority lowPriority = new LowPriority();
        hightPriority.setPriority(Thread.MAX_PRIORITY);//高优先级
        lowPriority.setPriority(Thread.MIN_PRIORITY);//低优先级
        lowPriority.start();
        hightPriority.start();
    }
    //HightPriority is complete
    //LowPriority is complete

}
posted @ 2019-08-20 16:30  fly_bk  阅读(371)  评论(0编辑  收藏  举报