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());
}
}
上述代碼示例將產生以下結果 -
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
¥ 我要打賞
糾錯/補充
收藏
加QQ群啦,易百教程官方技術學習群
注意:建議每個人選自己的技術方向加群,同一個QQ最多限加 3 個群。
總結
以上是生活随笔為你收集整理的java判断优先级代码,Java如何查看线程的优先级?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OJ1087: 获取出生日期(多实例测试
- 下一篇: Java中after注解_Spring(