zoukankan      html  css  js  c++  java
  • 线程优先级

    public class Demo10Priorityt {
    
        public static void main(String[] args) {
            PrioritytThread prioritytThread = new PrioritytThread();
    
            // 如果8核CPU处理3线程,无论优先级高低,每个线程都是单独一个CPU执行,就无法体现优先级
            // 开启10个线程,让8个CPU处理,这里线程就需要竞争CPU资源,优先级高的能分配更多的CPU资源
            for (int i = 0; i < 10; i++) {
                Thread t = new Thread(prioritytThread, "线程" + i);
                if (i == 1) {
                    t.setPriority(10);
                }
                if (i == 2) {
                    t.setPriority(1);
                }
                t.setDaemon(true);
                t.start();
            }
    
            try {
                Thread.sleep(1000l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            System.out.println("线程1总计:" + PrioritytThread.count1);
            System.out.println("线程2总计:" + PrioritytThread.count2);
        }
    
        static class PrioritytThread implements Runnable {
            public static Integer count1 = 0;
            public static Integer count2 = 0;
    
            public void run() {
                while (true) {
                    if ("线程1".equals(Thread.currentThread().getName())) {
                        count1++;
                    }
                    if ("线程2".equals(Thread.currentThread().getName())) {
                        count2++;
                    }
    
                    if (Thread.currentThread().isInterrupted()) {
                        break;
                    }
                }
            }
        }
    }
  • 相关阅读:
    linux中nc命令
    Centos6.5 安装zabbix3(收藏,非原创)
    紀念
    算法学习资源收集
    一道奇怪的求和题
    P5717 题解
    P1424 刷题记录
    P1888 题解
    P1422 刷题记录
    P1055 题解
  • 原文地址:https://www.cnblogs.com/angdh/p/15569561.html
Copyright © 2011-2022 走看看