gpt4 book ai didi

java - 为什么在此代码中优先级较低的线程比优先级较高的线程获得更长的 CPU 时间

转载 作者:行者123 更新时间:2023-12-02 06:54:08 25 4
gpt4 key购买 nike

我正在阅读来自 Java The complete Reference - Herbert Schildt (TATA McGRAW HILL) 的主题。在本示例表单中,优先级较低的线程应给出较低的值 clicks比具有更高优先级的线程。但我得到了什么

low-priority thread 3000255895
hi-priority thread 2857361716

根据书,它是这样的(差异)

low-priority thread 4408112
hi-priority thread 589626904

代码

class Clicker implements Runnable {
long click = 0;
Thread t;
private volatile boolean running = true;

public Clicker (int p) {
t = new Thread(this);
t.setPriority(p);
}

public void run() {
while(running) {
click++;
}
}

public void stop() {
running = false;
}

public void start() {
t.start();
}
}

class Priority {
public static void main(String args[]) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker hi = new Clicker(Thread.NORM_PRIORITY + 2);
Clicker lo = new Clicker(Thread.NORM_PRIORITY - 2);
hi.start();
lo.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("main thread interrupted");
}

lo.stop();
hi.stop();

try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {
System.out.println("interrupted exception catched in main");
}

System.out.println("low-priority thread " + lo.click);
System.out.println("hi-priority thread " + hi.click);
}
}

我正在使用Intel® Core™ i3-2310M CPU @ 2.10GHz × 4 with Ubuntu 13.04

Thread.sleep(50000);

low-priority thread 14849893875
hi-priority thread 14224080358

差别不是很大吗

我也尝试使用优先级 HI = 10 和 LOw = 1,但结果几乎相同。

更新:使用 100 个线程 50 运行后 priority = 150 with priority = 10结果是

这里time = clicks values

high = 82 time = 110117529
low = 83 time = 102549208
high = 84 time = 110905795
low = 85 time = 99100530
high = 86 time = 105012756
low = 87 time = 110195297
high = 88 time = 102820088
low = 89 time = 97814606
high = 90 time = 99990839
low = 91 time = 102326356
high = 92 time = 98656119
low = 93 time = 98127243
high = 94 time = 97097823
low = 95 time = 103604394
high = 96 time = 93632744
low = 97 time = 99032550
high = 98 time = 103879116
low = 99 time = 97179029

有 1000 个线程 Take 4-5 min. to complete, all 4 cores at 100%

low =  977 time = 23593983
high = 978 time = 23998970
low = 979 time = 23879458
high = 980 time = 22775297
low = 981 time = 21297504
high = 982 time = 22464600
low = 983 time = 20301501
high = 984 time = 19073992
low = 985 time = 19450707
high = 986 time = 19317769
low = 987 time = 18454648
high = 988 time = 17901939
low = 989 time = 16976661
high = 990 time = 17360728
low = 991 time = 16813824
high = 992 time = 15531501
low = 993 time = 14659965
high = 994 time = 12833364
low = 995 time = 13708670
high = 996 time = 13348568
low = 997 time = 12947993
high = 998 time = 12749436
low = 999 time = 9804643

每个线程的输出不可预测。

最佳答案

我怀疑示例数据在一定程度上取决于您的平台和作者的平台(该示例是否在单核上运行?)。 JVM 线程优先级是特定于操作系统/平台的。来自 this interesting document

Linux priorities

Under Linux, you have to go through more hoops to get thread priorities to function at all, although in the end, they may be more useful than under Windows. In Linux:

  • thread priorities only work as of Java 6 onwards;
  • for them to work, you must be running as root (or with root privileges via setuid);
  • the JVM parameter -XX:UseThreadPriorities must be included

因此请检查您的 Java 版本、Java 调用和权限。我怀疑(!)您只满足这些标准之一(您的 Java/JVM 版本)。

关于java - 为什么在此代码中优先级较低的线程比优先级较高的线程获得更长的 CPU 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17620393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com