Java Thread Priority Has No Effect

Java Thread priority has no effect

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.

Priorities are not working in threads in java

Thread priority doesn't do what you think it does

  • it is a hint to the OS which it can't ignore esp if you are not an administrator or root.
  • it only makes a difference if you have full utilisation of your CPUs. If all your CPUs are busy, it can use priority to decide how much time each one gets. If you have free CPUs every thread can run regardless of priority.
  • the time it takes to start a thread is not instant. A thread can take 0.1 ms to 10 ms to start, and in that time a thread can print many thousands of lines of output.

NOTE: Java uses native threads (in almost every modern JVM) which means it is the OS not Java which does the scheduling.

Thread priorities no effect

There are several things to keep in mind when working with Java threads.

  1. The rules for thread priorities are highly system-dependent. When
    the virtual machine relies on the thread implementation of the host
    platform, the thread scheduling is at the mercy of that thread
    implementation.
  2. Rule of thumb: 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 thread priority only to affect scheduling policy for
    efficiency purposes. Do not rely on it for algorithm correctness.
  3. What happens if there is more than one runnable thread with the same
    (highest) priority? One of the highest priority threads gets picked.
    It is completely up to the thread scheduler how to arbitrate between
    threads of the same priority. The Java programming language gives no
    guarantee that all of the threads get treated fairly.

The above being said, I don't see anything abnormal with the following output on my Windows 10 (Java 8) machine:

Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_1
Thread_3
--------
Thread_1 ended with: 34
--------
--------
Thread_2
Thread_3 ended with: 1
--------
--------
Thread_2 ended with: 1
--------

Have a look at this for further details.

I wrote this code to check the priority in java threads.But It doesn't give the priority in every time this code execute

If there are more cores than number of threads then there would not be any effect as all threads can be given CPU . Also even it seems like, but all threads are not deployed at same time.



Related Topics



Leave a reply



Submit