+ 1

setPriority() MIN and MAX

Regarding the code attached to this question, no matter what is set for the setPriority() parameter, the first declared thread object (name.start()) will run like 4 out of 5 times prior to the second thread object with the higher priority! My question is, is it because there is no control over the threads overall? Is it because of the platform that I am trying to run this code on and the resource sharing architecture of it? or am I doing somethnig wrong here? https://code.sololearn.com/cfYeFhZweKLD/?ref=app PS: Revieweed Minho's kind answer to this question, checked out the getPriority() and set it for each thread in their class, the performance of the code is much better now! However, I still get random mixed up outputs after running the code mutple times in a row but it's much better now. Still need to do more study on the topic! Thank you Minho!

18th Jan 2021, 11:03 PM
M Irdmousa
M Irdmousa - avatar
2 odpowiedzi
+ 3
Use the getPriority () method to check the priority of a thread. Your currently running thread is main and you're setting the priority of the main thread to first MIN_PRIORITY and then MAX_PRIORITY. So, System.out.println (Thread.currentThread().getPriority()); // output : 10 But the priority of Thread-0 and Thread-1 are set to the default priority 5. Check it, System.out.println (name.getPriority()); // output : 5 System.out.println (welcome.getPriority()); // output : 5 🔴 EDIT : Kindly read the answer provided by Elizabeth Kelly in this code 👇 https://code.sololearn.com/c9ckWvwXiXcZ/?ref=app It'll clear the doubts.
19th Jan 2021, 6:48 AM
Minho
Minho - avatar
+ 2
Something similar to me.... https://www.sololearn.com/Discuss/2672570/?ref=app Using searching... For simple operations don't rely on threads.. It depends on OS support.. Once look at this : hope it helps.. https://www.sololearn.com/Discuss/2211828/?ref=app
25th Jan 2021, 5:05 PM
David Ordás
David Ordás - avatar