SetPriority () method is not working correctly

I am doing the example code here . I am getting 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

      

Seems to be setPriority

wrong. Why?

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

+1


source to share


2 answers


The example you gave doesn't work.

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

  • In addition, the prioritization behavior of the thread scheduler is platform-dependent, not least because on many platforms, thread scheduling is done by the OS, not Java.

Here are a few selected Oracle references that address thread priorities:



  • Recommended encoding methods: not dependent on stream priorities
  • Java Themes Issues - which says:

    The thread priorities available for Java threads in the native threading JVM should be considered a hint to the scheduler, especially if threads are computationally involved. The number of processors available to a process is dynamic and unpredictable. Therefore, trying to use priorities for scheduling execution on any multitasking multiprocessor system is unlikely to succeed.

In addition to this material and material on how Java priorities map to native thread priorities, Oracle's documentation largely diminishes thread priorities for Java SE and what they mean. Of course, there is no attempt to pinpoint the behavior.

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

+4


source


I am assuming you have a multi-core processor.

Thread priority only affects thread scheduling, that is, it helps the system decide which threads will run next if there are multiple threads waiting to run.

But in your case, each thread is assigned to a different core. There are no waiting threads and no scheduling is required. All six threads are indeed running in parallel and thread priority has no effect.



If you want to see the effect of thread priority, tweak the main method slightly to create enough high priority threads to take up all of your processor cores. Then scheduling the role and priority of the thread plays a role:

public static void main(String[] args) {
    for (int i = 0; i < 5; i++)
        new SimplePriorities(Thread.MIN_PRIORITY);

    // create enough high priority threads to occupy all CPU cores:
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++)
        new SimplePriorities(Thread.MAX_PRIORITY);
}

      

+1


source







All Articles