14

This is a test about thread priority. The code is from Thinking in Java p.809

import java.util.concurrent.*;

public class SimplePriorities implements Runnable {
    private int countDown = 5;
    private volatile double d; // No optimization
    private int priority;

    public SimplePriorities(int priority) {
        this.priority = priority;
    }

    public String toString() {
        return Thread.currentThread() + ": " + countDown;
    }

    public void run() {
        Thread.currentThread().setPriority(priority);
        while (true) {
            // An expensive, interruptable operation:
            for (int i = 1; i < 10000000; i++) {
                d += (Math.PI + Math.E) / (double) i;
                if (i % 1000 == 0)
                    Thread.yield();
            }
            System.out.println(this);
            if (--countDown == 0)
                return;
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++)
            exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
        exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
        exec.shutdown();
    }
}

I wonder why I can't get a regular result like:

Thread[pool-1-thread-6,10,main]: 5 
Thread[pool-1-thread-6,10,main]: 4 
Thread[pool-1-thread-6,10,main]: 3 
Thread[pool-1-thread-6,10,main]: 2 
Thread[pool-1-thread-6,10,main]: 1 
Thread[pool-1-thread-3,1,main]: 5 
Thread[pool-1-thread-2,1,main]: 5 
Thread[pool-1-thread-1,1,main]: 5 
Thread[pool-1-thread-5,1,main]: 5 
Thread[pool-1-thread-4,1,main]: 5 
...

but a random result (every time I run it changes):

Thread[pool-1-thread-2,1,main]: 5
Thread[pool-1-thread-3,1,main]: 5
Thread[pool-1-thread-4,1,main]: 5
Thread[pool-1-thread-2,1,main]: 4
Thread[pool-1-thread-3,1,main]: 4
Thread[pool-1-thread-1,1,main]: 5
Thread[pool-1-thread-6,10,main]: 5
Thread[pool-1-thread-5,1,main]: 5
...

I use i3-2350M 2C4T CPU with Win 7 64 bit JDK 7. Does it matter ?

Cory Lee
  • 185
  • 1
  • 2
  • 8
  • 2
    I believe the problem is that you're explicitly yielding, which basically tells the OS, "I don't care what my priority is, let another thread run for a bit." – Harry Johnston Aug 20 '12 at 20:44
  • Most answers here mention that Thread priority is unreliable. But thread priority DOES HAVE an effect (at least on my system). The problem is that YOUR TEST CODE IS WRONG (!). See my answer below. – Christian Fries Aug 21 '13 at 05:49

12 Answers12

24

Java Thread priority has no effect

Thread priorities are highly OS specific and on many operating systems often have minimal effect. Priorities help to order the threads that are in the run queue only and will not change how often the threads are run in any major way unless you are doing a ton of CPU in each of the threads.

Your program looks to use a lot of CPU but unless you have fewer cores than there are threads, you may not see any change in output order by setting your thread priorities. If there is a free CPU then even a lower priority thread will be scheduled to run.

Also, threads are never starved. Even a lower priority thread will given time to run quite often in such a situation as this. You should see higher priority threads be given time sliced to run more often but it does not mean lower priority threads will wait for them to finish before running themselves.

Even if priorities do help to give one thread more CPU than the others, threaded programs are subject to race conditions which help inject a large amount of randomness to their execution. What you should see however, is the max priority thread is more likely to spit out its 0 message more often than the rest. If you add the priority to the println(), that should become obvious over a number of runs.

It is also important to note that System.out.println(...) is synchronized method that is writing IO which is going to dramatically affect how the threads interact and the different threads are blocking each other. In addition, Thread.yield(); can be a no-op depending on how the OS does its thread scheduling.

but a random result (every time I run it changes):

Right. The output from a threaded program is rarely if ever "perfect" because by definition the threads are running asynchronously. We want the output to be random because we want the threads to be running in parallel independently from each other. That is their power. If you expecting some precise output then you should not be using threads.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gray
  • 115,027
  • 24
  • 293
  • 354
  • I just reached your answer. Are you sure you are telling truth? Can you look at paragraph 3 which you wrote (starting with Also, threads are never starved...). Because I found this from microsoft site: ''As long as a thread with a higher priority is available to run, lower priority threads do not get to execute. When there are no more runnable threads at a given priority, the scheduler moves to the next lower priority and schedules the threads at that priority for execution.'' Link:https://docs.microsoft.com/en-us/dotnet/standard/threading/scheduling-threads How do I learn this now? :) – Stefan Jankovic Jul 29 '20 at 21:03
  • Telling the truth @StefanJankovic? Well I'm not trying to lie. I suspect that the way the JVM schedules its threads does not use these priorities because they seem draconian. That said, I'll edit my answer to point out that it is highly OS specific. – Gray Aug 04 '20 at 23:46
  • How about writing a test program. It should be easy to test thread starvation @StefanJankovic. – Gray Aug 04 '20 at 23:47
14

Thread priority is implementation dependent. In particular, in Windows:

Thread priority isn't very meaningful when all threads are competing for CPU. (Source)

The book "Java Concurrency in Practice" also says to

Avoid the temptation to use thread priorities, since they increase platform dependence and can cause liveness problems. Most concurrent applications can use the default priority for all threads.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
12

Thread priority does not guarantee execution order. It comes into play when resources are limited. If the System is running into constraints due to memory or CPU, then the higher priority threads will run first. Assuming that you have sufficient system resources (which I would assume so for a simple program and the system resources you posted), then you will not have any system constraints. Here is a blog post (not my post) I found that provides more information about it: Why Thread Priority Rarely Matters.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
9

Let's keep it simple and go straight to the source ...

Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.

  • from the Java Language Specification (2nd Edition) p.445.

Also ...

Although thread priorities exist in Java and many references state that the JVM will always select one of the highest priority threads for scheduling [52, 56, 89], this is currently not guaranteed by the Java language or virtual machine specifications [53, 90]. Priorities are only hints to the scheduler [127, page 227].

  • from Testing Concurrent Java Components (PhD Thesis, 2005) p. 62.

  • Reference 127, page 227 (from the excerpt above) is from Component Software: Beyond Object-Oriented Programming (by C. Szyperski), Addison Wesley, 1998.

In short, do not rely on thread priorities.

xagyg
  • 9,562
  • 2
  • 32
  • 29
5

Thread priority is only a hint to OS task scheduler. Task scheduler will only try to allocate more resources to a thread with higher priority, however there are no explicit guarantees.

In fact, it is not only relevant to Java or JVM. Most non-real time OS use thread priorities (managed or unmanaged) only in a suggestive manner.

Jeff Atwood has a good post on the topic: "Thread Priorities are Evil"

Here's the problem. If somebody begins the work that will make 'cond' true on a lower priority thread (the producer), and then the timing of the program is such that the higher priority thread that issues this spinning (the consumer) gets scheduled, the consumer will starve the producer completely. This is a classic race. And even though there's an explicit Sleep in there, issuing it doesn't allow the producer to be scheduled because it's at a lower priority. The consumer will just spin forever and unless a free CPU opens up, the producer will never produce. Oops!

oleksii
  • 35,458
  • 16
  • 93
  • 163
4

As mentioned in other answers, Thread priority is more a hint than a definition of a strict rule.

That said, even if priority would be considered in a strict(er) way, your test-setup would not lead to the result which you describe as "expected". You are first creating 5 threads having equally low priority and one thread having high priority.

  • The CPU you are using (i3) has 4 native threads. So even if high priory would imply that the thread runs without interruption (which is not true), the low priority threads will get 3/4 of the cpu power (given that no other task is running). This CPU power is allocated to 5 threads, thus the low priority threads will run at 4 * 3/4 * 1/5 = 3/5 times the speed of the high priority thread. After the high priority thread has completed, the low priority threads run at 4/5 of the speed of the high priority thread.

  • You are launching the low priority threads before the high priority threads. Hence they start a bit earlier. I expect that in most systems "priority" is not implemented down to the nanosecond. So the OS will allow that a threads runs "a little longer" until it switches to another thread (to reduce the impact of switching cost). Hence, the result depends a lot on how that switching is implemented and how big the task is. If the task is small, it could be that no switching will take place, and in your example all the low priority threads finish first.

  • These calculations assumed that high and low priority where interpreted as extrem cases. In fact, priority is rather something like "in n out of m cases prefer this" with variable n and m.

  • You have a Thread.yield in your code! This will pass execution to another thread. If you do that too often, it will result in all threads getting equal CPU power.

Hence, I would not expect the output you mentioned in your question, namely that the high priority thread finishes first and then the low priority thread finish.

Indeed: With the line Thread.yield I have the result that each thread get the same CPU time. Without the line Thread.yield and increasing the number of calculations by a factor of 10 and increasing the number of low prio threads by a factor of 10, I get an expected result, namely that the high prio thread finishes earlier by some factor (which depends on the number of native CPU threads).

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
1

I believe the OS is free to disregard Java thread priority.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
1

Thread priority is a hint (which can be ignored) such that when you have 100% CPU, the OS know you want to prefer some threads over others. The thread priority must be set before the thread is started or it can be ignored.

On Windows you have to be Administrator to set the thread priority.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • In addition: His test code is wrong in some aspects (see my answer). For me on OS X a modified version did indeed give "an" expected result.... – Christian Fries Aug 19 '13 at 08:13
1

Several things to consider:

Dariusz
  • 21,561
  • 9
  • 74
  • 114
0

Some OS doesn't provide proper support for Thread priorities.

Satyendra
  • 203
  • 2
  • 7
0

You have got to understand that different OS deal with thread priorities differently. For example Windows uses a pre-emptive time-slice based scheduler which gives a bigger time slice to higher priority threads where as some other OS (very old ones) use non pre-emptive scheduler where higher priority thread is executed entirely before lower priority thread unless the higher priority thread goes to sleep or does some IO operation etc.

That is why it is not guaranteed that the higher priority thread completely executes, it infact executes for more time than low priority threads so that is why your outputs are ordered in such a way.

In order to know how Windows handles multithreading please refer the below link:

https://learn.microsoft.com/en-us/windows/win32/procthread/multitasking

0

High-priority thread doesn't stop low-priority thread until it's done. High-priority doesn't make something faster either, if it did, we'd always make everything high-priority. And low-priority doesn't make things slower or we'd never use it.

As I understand you are misunderstanding that rest of the system should be idle and just let the highest-priority thread work while the rest of the system's capacity is wasted.