简体   繁体   English

线程内线程的优先级

[英]Prioritization of threads within threads

Suppose you have a program that starts two threads a and b , and b starts another ten threads of its own. 假设你有一个程序启动两个线程ab ,而b启动另外十个自己的线程。 Does a receive half of the available "attention" while b and its threads share the other half, or do they all share equally? 难道a收到一半的可用的“关注”,而b和它的线程共享的另一半,还是他们都平等地分担? If the answer is the latter by default, how could you achieve the former? 如果答案是默认的后者,你怎么能实现前者? Thanks! 谢谢!

There are lots of nice documentation on this topic. 关于这个主题有很多很好的文档。 One such is this . 其中之一就是这个

When a Java thread is created, it inherits its priority from the thread that created it. 创建Java线程时,它会从创建它的线程继承其优先级。 You can also modify a thread's priority at any time after its creation using the setPriority() method. 您还可以使用setPriority()方法在创建线程后随时修改线程的优先级。 Thread priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY (constants defined in the Thread class). 线程优先级是MIN_PRIORITYMAX_PRIORITY (Thread类中定义的常量)之间的整数。 The higher the integer, the higher the priority. 整数越高,优先级越高。 At any given time, when multiple threads are ready to be executed, the runtime system chooses the "Runnable" thread with the highest priority for execution. 在任何给定时间,当准备好执行多个线程时,运行时系统选择具有最高优先级的“Runnable”线程来执行。 Only when that thread stops, yields, or becomes "Not Runnable" for some reason will a lower priority thread start executing. 只有当该线程因某种原因停止,产生或变为“不可运行”时,优先级较低的线程才会开始执行。 If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. 如果两个具有相同优先级的线程正在等待CPU,则调度程序选择其中一个以循环方式运行。 The chosen thread will run until one of the following conditions is true: 选定的线程将运行,直到满足下列条件之一:

  1. A higher priority thread becomes "Runnable". 优先级较高的线程变为“Runnable”。
  2. It yields, or its run() method exits. 它会产生,或者它的run()方法退出。
  3. On systems that support time-slicing, its time allotment has expired. 在支持时间分片的系统上,其时间分配已过期。

At any given time, the highest priority thread is running. 在任何给定时间,最高优先级的线程正在运行。 However, this is not guaranteed. 但是,这不能保证。 The thread scheduler may choose to run a lower priority thread to avoid starvation. 线程调度程序可以选择运行较低优先级的线程以避免饥饿。 For this reason, use priority only to affect scheduling policy for efficiency purposes. 因此,出于效率目的,仅将优先级用于影响调度策略。 Do not rely on thread priority for algorithm correctness. 不要依赖线程优先级来保证算法的正确性。

Does a receive half of the available "attention" while b and its threads share the other half, or do they all share equally? 难道a收到一半的可用的“关注”,而b和它的线程共享的另一半,还是他们都平等地分担?

Neither. 都不是。 The proportion of time received by each thread is unspecified, and there's no reliable way to control it in Java. 每个线程收到的时间比例是未指定的,并且在Java中没有可靠的方法来控制它。 It is up to the native thread scheduler. 它取决于本机线程调度程序。

If the answer is the latter by default, how could you achieve the former? 如果答案是默认的后者,你怎么能实现前者?

You can't, reliably. 你不能,可靠。

The only thing that you have to influence the relative amounts of time each thread gets to run are thread priorities. 您必须影响每个线程运行的相对时间量的唯一因素是线程优先级。 Even they are not reliable or predictable. 即使它们不可靠或不可预测。 The javadocs simply say that a high priority thread is executed "in preference to" a lower priority thread. javadocs简单地说高优先级线程“优先于”低优先级线程执行。 In practice, it depends on how the native thread scheduler handles priorities. 实际上,它取决于本机线程调度程序如何处理优先级。

For more details: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/thread-priorities.html ... which includes information on how thread priorities on a range of platforms and Java versions. 有关更多详细信息,请访问: http//docs.oracle.com/javase/7/docs/technotes/guides/vm/thread-priorities.html ...其中包含有关各种平台和Java版本的线程优先级的信息。

One cannot say with surity the order in which the threads will be executed. 人们不能说有线程执行的顺序。 Thread Scheduler works as per its inbuilt algorithm which we cannot change. 线程调度程序按照我们无法更改的内置算法工作。 Thread Scheduler picks up any threads (Highest priority threads) from runnable pool and make it running. 线程调度程序从可运行池中获取任何线程(最高优先级线程)并使其运行。 We can only mention the priority in which scheduler should process our threads. 我们只能提到调度程序应该处理我们的线程的优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM