+ 4

Quiz - Threads

Hello! I'm trying to understand why the following solution does not work for the Java threads quiz. "Welcome" should output to the console before "Please enter your name." Any idea what is happening? class Main { public static void main(String[ ] args) { Name name = new Name(); //set priority Welcome welcome = new Welcome(); //set priority name.setPriority(1); welcome.setPriority(5); name.start(); welcome.start(); } } //extend the Thread class class Welcome extends Thread { public void run() { System.out.println("Welcome!"); } } //extend the Thread class class Name extends Thread { public void run() { System.out.println("Please enter your name"); } }

13th Jan 2021, 2:35 PM
ossrey
ossrey - avatar
2 Answers
+ 1
Hello, Sorry that your question went 3 days without a reply! Hopefully you already figured out the problem! But if not...here is your answer: For thread priorities in Java, the higher the number, the higher the priority. Therefore a thread with priority of 1 will be scheduled AFTER a thread with priority 5. Switch the priorities on your threads and then they will print in the order required.
16th Jan 2021, 4:20 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
0
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:10 PM
David OrdĂĄs
David OrdĂĄs - avatar