Java 线程转储优先级值与 linux 上的实际线程优先级不对应?
Posted
技术标签:
【中文标题】Java 线程转储优先级值与 linux 上的实际线程优先级不对应?【英文标题】:Java thread dump prio value doesn't correspond with real thread priority on linux? 【发布时间】:2012-05-12 22:35:25 【问题描述】:我打算在我的 Java 代码中使用线程优先级。 该应用程序应在我的 Linux 系统上运行:
>uname -a
Linux <host> 3.0.0-15-generic #26-Ubuntu SMP <date> x86_64 x86_64 x86_64 GNU/Linux
>java -version
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.1)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
在网上阅读了一些内容后,我现在使用以下命令启动我的测试应用程序:
sudo java -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=1 -jar ThreadPriorityTest.jar
测试应用程序由以下两个类组成:
package ch.mypackage;
public class CountingRunnable implements Runnable
private long count = 0;
private boolean goOn = true;
public long getCount()
return count;
public void stop()
goOn=false;
public void run()
for(long iteration=0;goOn&&iteration<Long.MAX_VALUE;++iteration)
++count;
package ch.mypackage;
public class PriorizedCountingThreads
private static final int NUM_MILLIS_TO_COUNT_FOR = 1*60*1000;
private static CountingRunnable[] runnables;
private static Thread[] threads;
public static void main(String[] args)
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
System.out.println("MIN_PRIORITY: "+Thread.MIN_PRIORITY);
System.out.println("MAX_PRIORITY: "+Thread.MAX_PRIORITY);
int numPriorityLevels = (Thread.MAX_PRIORITY-Thread.MIN_PRIORITY) + 1;
init(numPriorityLevels);
startThreads();
try
Thread.sleep(NUM_MILLIS_TO_COUNT_FOR);
catch (InterruptedException e)
e.printStackTrace();
stopRunnables();
printCounts();
private static void printCounts()
for (int i = 0; i < runnables.length; ++i)
System.out.println(threads[i].getName() + " has priority: " + threads[i].getPriority() + " and count:" + runnables[i].getCount());
private static void stopRunnables()
for (int i = 0; i < runnables.length; ++i)
runnables[i].stop();
private static void startThreads()
for (int i = 0; i < threads.length; ++i)
threads[i].start();
private static void init(int numPriorityLevels)
runnables = new CountingRunnable[numPriorityLevels];
threads = new Thread[runnables.length];
for (int i = 0; i < runnables.length; ++i)
int priority = i + 1;
runnables[i] = new CountingRunnable();
threads[i] = new Thread(runnables[i]);
threads[i].setPriority(priority);
threads[i].setName("PriorityThread_" + priority);
如果我让程序计数一分钟 (NUM_MILLIS_TO_COUNT_FOR=1601000),那么我会得到以下输出:
MIN_PRIORITY: 1
MAX_PRIORITY: 10
PriorityThread_1 has priority: 1 and count:12658044343
PriorityThread_2 has priority: 2 and count:19008431582
PriorityThread_3 has priority: 3 and count:30618946099
PriorityThread_4 has priority: 4 and count:34408365142
PriorityThread_5 has priority: 5 and count:36694025023
PriorityThread_6 has priority: 6 and count:40493710165
PriorityThread_7 has priority: 7 and count:42826305342
PriorityThread_8 has priority: 8 and count:42203891414
PriorityThread_9 has priority: 9 and count:43128747383
PriorityThread_10 has priority: 10 and count:43416371500
根据此输出,优先级似乎具有预期的影响! 但是,如果我使用“jstack”或“kill -s QUIT”生成线程转储,则会得到以下输出,这意味着每个线程都有相同的优先级(prio=10):
"PriorityThread_10" prio=10 tid=0x00007ff7e406f800 nid=0x12e6 runnable [0x00007ff7e2562000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_9" prio=10 tid=0x00007ff7e406d800 nid=0x12e5 runnable [0x00007ff7e2663000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_8" prio=10 tid=0x00007ff7e406b000 nid=0x12e4 runnable [0x00007ff7e2764000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_7" prio=10 tid=0x00007ff7e4069000 nid=0x12e3 runnable [0x00007ff7e2865000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_6" prio=10 tid=0x00007ff7e4067000 nid=0x12e2 runnable [0x00007ff7e2966000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_5" prio=10 tid=0x00007ff7e4065000 nid=0x12e1 runnable [0x00007ff7e2a67000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_4" prio=10 tid=0x00007ff7e4063000 nid=0x12e0 runnable [0x00007ff7e2b68000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_3" prio=10 tid=0x00007ff7e4061000 nid=0x12df runnable [0x00007ff7e2c69000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_2" prio=10 tid=0x00007ff7e405d000 nid=0x12de runnable [0x00007ff7e2d6a000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
"PriorityThread_1" prio=10 tid=0x00007ff7e4049800 nid=0x12dd runnable [0x00007ff7e2e6b000]
java.lang.Thread.State: RUNNABLE
at ch.mypackage.CountingRunnable.run(CountingRunnable.java:17)
at java.lang.Thread.run(Thread.java:679)
如果我在 Windows 机器上执行相同操作,则根据我找到的优先级映射 here,prio 值是正确的。
那么,这是 jstack 中的一个错误,还是我做错了什么?
如果我执行“top | grep java”,我会得到以下信息:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3394 root 20 0 4444m 15m 8376 S 789 0.1 0:47.52 java
这意味着主线程的优先级为 20,而 "top -H | grep java" 的输出结果如下:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3457 root 15 -5 4444m 15m 8384 R 99 0.1 0:08.60 java
3456 root 16 -4 4444m 15m 8384 R 97 0.1 0:08.41 java
3455 root 17 -3 4444m 15m 8384 R 93 0.1 0:08.42 java
3454 root 18 -2 4444m 15m 8384 R 97 0.1 0:08.27 java
3453 root 19 -1 4444m 15m 8384 R 97 0.1 0:07.50 java
3452 root 20 0 4444m 15m 8384 R 51 0.1 0:07.44 java
3451 root 21 1 4444m 15m 8384 R 35 0.1 0:04.83 java
3450 root 22 2 4444m 15m 8384 R 99 0.1 0:04.78 java
3449 root 23 3 4444m 15m 8384 R 95 0.1 0:07.47 java
3448 root 24 4 4444m 15m 8384 R 18 0.1 0:02.85 java
这表明java线程的优先级确实会影响操作系统线程的优先级。
但是 jstack 在 prio=10 中的值为 10 是从哪里来的呢? 它只是一个任意值吗?
【问题讨论】:
请注意,Java 线程不一定对应于系统进程/线程(例如,它可能是在内部进行调度的单进程 JVM),因此 Java 的线程优先级不一定与 OS 线程匹配优先级。 无论 Java 线程是否对应于 OS 线程,Java 优先级在数值上都未指定为等于 OS 级别。 @Thomas AFAIK,openJDK 根本不使用绿色线程,无论平台如何。 另见***.com/questions/128039/… 【参考方案1】:Java 中的线程优先级是一个非常敏感的话题... 你可以在这里找到关于 StackOverlow 的有趣帖子:https://***.com/a/2170304/1343096
基本上尝试将您的优先级政策更改为 42 :
-XX:ThreadPriorityPolicy=42
如果它对您有用,请告诉我们!
您可以通过以下链接找到所有解释。
【讨论】:
【参考方案2】:Java 7 docs 状态
注意 - 此实用程序不受支持,可能会或可能不会在 JDK 的未来版本中可用。
所以我会假设jstack
的输出可能不准确,因为 linux 上的实现细节可能已经改变。
htop -H
报告的值与您在问题中包含的this article 一致,并且是在 linux 内核中调度 java 进程的值。
如果您需要将 jstack 的输出映射到 top -H
报告的线程,您只需将 nid
从 jstack
转换为十进制或将 pid
从 top -H
转换为十六进制。 See this article
关于您的源代码:当我运行它时,该程序不会终止。编写生产多任务代码时的三个提示:
如果您知道一切都已完成(如从main
调用 printCounts()
之后),请不要犹豫使用 System.exit(0);
以确保所有线程都已终止。
如果您想在并行执行期间测试代码的行为,通常最好先创建所有Thread
s 并让它们同步工作。在共享的CountDownLatch
或CyclicBarrier
上调用await()
作为worker 的第一条语句通常可以很好地完成这个技巧。
虽然boolean
的读写按照Java tutorial 是原子的,但我建议将像goOn
字段这样的触发器变量声明为易失性,因为这也会影响编译器优化代码的方式。李>
使用volatile
或System.exit(0)
,您的基准测试正常终止。
【讨论】:
以上是关于Java 线程转储优先级值与 linux 上的实际线程优先级不对应?的主要内容,如果未能解决你的问题,请参考以下文章