zoukankan      html  css  js  c++  java
  • Java如何查看线程的优先级?

    Java编程中,如何查看线程的优先级?

    以下示例演示如何使用Thread类的getPriority()方法检查线程的优先级。

    package com.yiibai;
    
    public class ThreadPriorityLevel extends Object {
        private static Runnable makeRunnable() {
            Runnable r = new Runnable() {
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        Thread t = Thread.currentThread();
                        System.out.println("in run() - priority = " + t.getPriority() + ", name = " + t.getName());
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException x) {
                        }
                    }
                }
            };
            return r;
        }
    
        public static void main(String[] args) {
            System.out.println("in main() - Thread.currentThread(). getPriority()=" + Thread.currentThread().getPriority());
            System.out.println("in main() - Thread.currentThread().getName()=" + Thread.currentThread().getName());
    
            Thread threadA = new Thread(makeRunnable(), "threadA");
            threadA.start();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException x) {
            }
            System.out.println("in main() - threadA.getPriority() = " + threadA.getPriority());
        }
    }
    
    Java

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

    in main() - Thread.currentThread(). getPriority()=5
    in main() - Thread.currentThread().getName()=main
    in run() - priority = 5, name = threadA
    in run() - priority = 5, name = threadA
    in main() - threadA.getPriority() = 5
    in run() - priority = 5, name = threadA
    in run() - priority = 5, name = threadA
    in run() - priority = 5, name = threadA
  • 相关阅读:
    数据倾斜原理及解决方案
    删除emp_no重复的记录,只保留最小的id对应的记录
    理解HBase面向列存储
    给数据库用户授权(对象多为系统表,如dba可以查看的表)
    SpringBoot里的一些注解
    01背包
    【转】简说GNU, GCC and MinGW (Lu Hongling)
    费马小定理
    欧拉定理
    【转】C中的静态存储区和动态存储区
  • 原文地址:https://www.cnblogs.com/borter/p/9613454.html
Copyright © 2011-2022 走看看