Reputation: 87
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Child Thread:" + i);
}
}
}
public class ThreadPriorityProperDemo {
public static void main(String[] args) {
MyThread t=new MyThread();
t.setPriority(10);
System.out.println("Main Priority:"+Thread.currentThread().getPriority());
System.out.println("Child Priority:"+t.getPriority());
System.out.println("-----------------------------------");
t.start();
for (int i = 0; i < 10; i++) {
System.out.println("Parent Thread:" + i);
}
}
}
The ideal output should be :
Main Priority:5
Child Thread:0
Child Thread:1
Child Thread:2
Child Thread:3
Child Thread:4
Child Thread:5
Child Thread:6
Child Thread:7
Child Thread:8
Child Thread:9
Parent Thread:0
Parent Thread:1
Parent Thread:2
Parent Thread:3
Parent Thread:4
Parent Thread:5
Parent Thread:6
Parent Thread:7
Parent Thread:8
Parent Thread:9
But I am getting mixed output.. Why? I am on Ububtu 16.04 LTS
Upvotes: 0
Views: 750
Reputation: 131556
You have a element of answer in the Oracle documentation.
The JVM defines a range of ten logical priorities for Java threads, including:
java.lang.Thread.MIN_PRIORITY = 1
java.lang.Thread.NORM_PRIORITY = 5
java.lang.Thread.MAX_PRIORITY = 10
...
A JVM is free to implement priorities in any way it chooses, including ignoring the value.
Besides, even if the JVM fully honors the priority, a multi-core CPU can run both threads in parallel.
The priority is visible only if the number of active threads exceeds the number of process that may be run concurrently by your CPU (for hyperthreading CPU = Core * number of thread by Core).
At last, setting priorities for short tasks or setting priorities that may not really be applied because the condition explained before is not true (you have less concurrent threads than threads that your CPU is able to handle) will have bad consequences on the performance of the application :
Calling the Thread.setPriority method may be an expensive operation. Frivolous priority adjustments can reduce performance.
Upvotes: 2
Reputation: 1470
This is in reference to OCA/OCP JAVA SE 7 Programmer I & II Study Guide by Kathy Sierra and Bert Bates.
The setPriority() method is used on Thread objects to give threads a priority of between 1 (low) and 10 (high), although priorities are not guaranteed, and not all JVMs recognize 10 distinct priority levels—some levels may be treated as effectively equal.
Also,
In most cases, the running thread will be of equal or greater priority than the highest-priority threads in the pool. This is as close to a guarantee about scheduling as you'll get from the JVM specification, so you must never rely on thread priorities to guarantee the correct behavior of your program.
And this last one :
Don't rely on thread priorities when designing your multithreaded application. Because thread-scheduling priority behavior is not guaranteed...
If you just wish to obtain the desired output you can try using JOIN():
public class ThreadPriorityProperDemo {
public static void main(String[] args) {
MyThread t = new MyThread();
t.setPriority(10);
System.out.println("Main Priority:" + Thread.currentThread().getPriority());
System.out.println("Child Priority:" + t.getPriority());
System.out.println("-----------------------------------");
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("Parent Thread:" + i);
}
}
}
Upvotes: 0
Reputation: 725
Also, remember that it is very likely that your computer has multiple cores, so it will execute multiple threads simultaneously. execute the following code:
int cores = Runtime.getRuntime().availableProcessors();
In my computer, I get 8 cores. So Java can execute up to 8 threads simultaneously. If you want to execute tasks that with different priorities, a good approach is to use two different thread pools. for example:
ExecutorService executorLowPriority = Executors.newFixedThreadPool(2);
ExecutorService executorHighPriority = Executors.newFixedThreadPool(6);
Upvotes: 0