setPriority()方法无法正常工作 [英] setPriority() method doesn't work properly

查看:422
本文介绍了setPriority()方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处处获取示例代码。我得到结果:

I do the sample code from here. I get the result:

Thread[Thread-3,1,main]: 5
Thread[Thread-0,10,main]: 5
Thread[Thread-2,1,main]: 5
Thread[Thread-1,1,main]: 5
Thread[Thread-4,1,main]: 5
Thread[Thread-0,10,main]: 4
Thread[Thread-3,1,main]: 4
Thread[Thread-2,1,main]: 4
Thread[Thread-0,10,main]: 3
Thread[Thread-5,1,main]: 5
Thread[Thread-3,1,main]: 3
Thread[Thread-2,1,main]: 3
Thread[Thread-1,1,main]: 4
Thread[Thread-5,1,main]: 4
Thread[Thread-3,1,main]: 2
Thread[Thread-2,1,main]: 2
Thread[Thread-5,1,main]: 3
Thread[Thread-3,1,main]: 1
Thread[Thread-4,1,main]: 4
Thread[Thread-0,10,main]: 2
Thread[Thread-2,1,main]: 1
Thread[Thread-5,1,main]: 2
Thread[Thread-4,1,main]: 3
Thread[Thread-5,1,main]: 1
Thread[Thread-1,1,main]: 3
Thread[Thread-0,10,main]: 1
Thread[Thread-4,1,main]: 2
Thread[Thread-1,1,main]: 2
Thread[Thread-4,1,main]: 1
Thread[Thread-1,1,main]: 1

似乎 setPriority 不工作正常。为什么?

It seems that setPriority doesn't work properly. Why?

如何修改示例代码以查看属性的效果。

How can I modify the sample code to see the effect of property.

推荐答案

你找到的例子已经破了。

The example you have found is broken.


  • 正如其他问题和评论所指出的,如果你的JVM可以使用多个内核,然后多个线程可以并行运行。当发生这种情况时,它不会只是运行的优先级最高的可运行线程。

  • As other questions and comments have pointed out, if your JVM can use multiple cores, then multiple threads can run in parallel. When that happens, it won't be just the highest priority runnable thread that run.

最重要的是,线程调度程序的行为与优先级取决于平台,尤其是因为在很多平台上,线程调度是由OS而不是Java实现的。

On top of that, the behaviour of the thread scheduler with respect to priorities is dependent on the platform, not least because on a lot of platforms, thread scheduling is implemented by the OS rather than Java.

以下是一些涉及线程优先级的选定Oracle引用:

Here are some selected Oracle references that touch on thread priorities:


本机线程JVM上的Java线程可用的线程优先级应该被视为调度程序的提示,特别是如果线程是计算结合。进程可用的处理器数量是动态且不可预测的。因此,尝试使用优先级来安排在任何多任务,多处理器系统上执行不太可能成功。

The thread priorities available to Java threads on a native threaded JVM should be treated as hints to the scheduler, especially if the threads are compute-bound. The number of processors available to a process is dynamic and unpredictable. Therefore, an attempt to use priorities to schedule execution on any multi-tasked, multiprocessor system is not likely to succeed.


除了这种材料,以及关于Java优先级如何映射到本机线程优先级的材料之外,Oracle文档几乎没有提及Java SE的线程优先级及其含义。当然,没有尝试精确指定行为。

Apart from this kind of material, and material on how Java priorities map to native thread priorities, the Oracle documentation is pretty much silent on thread priorities for Java SE and what they mean. Certainly, there is no attempt to specify the behaviour precisely.

Java RT(实时)是一个不同的问题,但它不再是Oracle支持的产品线。 (AFAIK)。

Java RT (realtime) is a different issue, but that is no longer an Oracle supported product line. (AFAIK).

这篇关于setPriority()方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆