+ 1

simple java question about setPriority

so why tf does this not work.. "Welcome!" is always supposed to be printed first, then "Please enter your name" (this program does not suppose to take an input btw.. just prints those two things out) class Main { public static void main(String[] args) { Name name = new Name(); //set priority to low name.setPriority(1); Welcome welcome = new Welcome(); //set priority to high welcome.setPriority(2); name.start(); welcome.start(); } } //extend the Thread class class Welcome extends Thread { @Override public void run() { setPriority(2); // still doesn't work System.out.println("Welcome!"); } } //extend the Thread class class Name extends Thread { @Override public void run() { setPriority(1); // also doesn't do anything.. System.out.println("Please enter your name"); } } then when I use Thread.currentThread().getPriority() on each run method, it shows their respective priority, so I don't get it.. for reference, this is java course practice lesson 59.2

14th Nov 2021, 7:53 PM
Preston
2 Réponses
0
setPriority doesn't guarantee that it will be set priority. It is like you say:"please set priority"(it might set and might not). See docs. I can't understand your code. why you use 2 threads for work that enough use just 1. first welcome then ask name or ask name then welcome. if you want use 2 threads in this order then look at synchronization. it is simple.
14th Nov 2021, 9:45 PM
Кервен Гурбангулыев
0
it depends on OS decision which is run first and how long, also if machine has more cpu (cores) and threads can runs parallel, priorities are ignored. jvm can prefers higher priorities to execute but OS can use its own thread strategy. jvm priorities are maped to OS priorities which are different ie 1,2 jvm can be same one in os or <5 are ignored as low
14th Nov 2021, 10:37 PM
zemiak