zoukankan      html  css  js  c++  java
  • Java如何获得运行线程的优先级?

    在Java编程中,如何获得运行线程的优先级?

    以下示例使用setPriority()方法打印正在运行的线程的优先级。

    package com.yiibai;
    
    public class GettingThreadPriority extends Thread {
        private int countDown = 5;
        private volatile double d = 0;
    
        public GettingThreadPriority(int priority) {
            setPriority(priority);
            start();
        }
    
        public String toString() {
            return super.toString() + ": " + countDown;
        }
    
        public void run() {
            while (true) {
                for (int i = 1; i < 100000; i++)
                    d = d + (Math.PI + Math.E) / (double) i;
                System.out.println(this);
                if (--countDown == 0)
                    return;
            }
        }
    
        public static void main(String[] args) {
            new GettingThreadPriority(Thread.MAX_PRIORITY);
            for (int i = 0; i < 5; i++) {
                new GettingThreadPriority(Thread.MIN_PRIORITY);
            }
        }
    }
    
    Java

    上述代码示例将产生以下结果 -

    Thread[Thread-0,10,main]: 5
    Thread[Thread-3,1,main]: 5
    Thread[Thread-0,10,main]: 4
    Thread[Thread-2,1,main]: 5
    Thread[Thread-3,1,main]: 4
    Thread[Thread-0,10,main]: 3
    Thread[Thread-0,10,main]: 2
    Thread[Thread-2,1,main]: 4
    Thread[Thread-3,1,main]: 3
    Thread[Thread-0,10,main]: 1
    Thread[Thread-2,1,main]: 3
    Thread[Thread-3,1,main]: 2
    Thread[Thread-1,1,main]: 5
    Thread[Thread-4,1,main]: 5
    Thread[Thread-2,1,main]: 2
    Thread[Thread-3,1,main]: 1
    Thread[Thread-1,1,main]: 4
    Thread[Thread-2,1,main]: 1
    Thread[Thread-4,1,main]: 4
    Thread[Thread-5,1,main]: 5
    Thread[Thread-1,1,main]: 3
    Thread[Thread-1,1,main]: 2
    Thread[Thread-4,1,main]: 3
    Thread[Thread-5,1,main]: 4
    Thread[Thread-1,1,main]: 1
    Thread[Thread-4,1,main]: 2
    Thread[Thread-5,1,main]: 3
    Thread[Thread-4,1,main]: 1
    Thread[Thread-5,1,main]: 2
    Thread[Thread-5,1,main]: 1
  • 相关阅读:
    Verilog之阻塞赋值非阻塞赋值
    Verilog之case语句
    Verilog 分频器
    kvm虚拟化存储管理(3)
    kvm虚拟机管理(2)
    Kvm 虚拟化介绍(1)
    云计算 大数据 人工智能
    openstack核心组件——cinder存储服务(11)
    openstack核心组件——horizon Web管理界面(10)
    openstack核心组件——neutron网络服务 抓取ip(9)
  • 原文地址:https://www.cnblogs.com/borter/p/9613436.html
Copyright © 2011-2022 走看看