Prioritizing Threads in Threads

Suppose you have a program that starts two threads a and b , and b starts ten more threads of its own. Does a get half the available “attention” while b does its threads share the other half, or are they all equally distributed? If the default answer is the last, how could you achieve the first? Thanks!

+6
source share
3 answers

Does a get half the available “attention” while b does its threads share the other half, or are they all equally distributed?

None. The fraction of time received by each thread is not defined, and there is no reliable way to manage it in Java. It depends on the scheduler of its own threads.

If the default answer is the last, how could you achieve the first?

You cannot, reliably.

The only thing you have to influence the relative amount of time that each thread runs is the thread priorities. Even they are not reliable or predictable. Javadocs simply says that a higher priority thread runs "in preference" of a lower priority thread. In practice, this depends on how the scheduler of its own threads handles the priorities.

More details: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/thread-priorities.html ..., which includes information on how to prioritize threads on different platforms and versions of Java.

+2
source

There is a lot of good documentation on this topic. One of these is this .

When a Java thread is created, it inherits its priority from the thread that created it. You can also change the priority of a stream at any time after creating it using the setPriority () method. Thread priorities are integers from MIN_PRIORITY and MAX_PRIORITY (constants defined in the Thread class). The higher the integer, the higher the priority. At any given time, when multiple threads are ready to be executed, the runtime system selects the “Runnable” thread with the highest priority for execution. Only when this thread stops, issues, or becomes "Not Runnable", for some reason, a downstream thread with a lower priority will start. If two threads of the same priority are waiting for the CPU, the scheduler selects one of them for a cyclic start. The selected thread will work until one of the following conditions is met:

  • A thread with a higher priority becomes "Runnable".
  • It returns or its run () method exits.
  • On systems that support time reference, it has expired.

At any given time, the thread with the highest priority is executed. However, this is not guaranteed. The thread scheduler can choose to start a thread with a lower priority to avoid starvation. For this reason, use priority to influence planning policies for effectiveness. Do not rely on thread priority for the correctness of the algorithm.

+5
source

You cannot say with certainty in what order the threads will be executed. The thread scheduler works according to a built-in algorithm that we cannot change. The thread scheduler collects any threads (Highest Priority Threads) from the starting pool and starts it. We can only mention the priority in which the scheduler should process our threads.

0
source

Source: https://habr.com/ru/post/943958/


All Articles