+ 2

Thread

It shall input first the message Welcome and after this "Please enter..". Can someone tell me what I have dons wrong? class Main { public static void main(String[ ] args) { Name name = new Name(); //set priority name.setPriority(1); Welcome welcome = new Welcome(); //set priority welcome.setPriority(2); 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"); } }

25th Mar 2021, 2:30 PM
Zotta
Zotta - avatar
10 ответов
+ 3
Hello Zotta I have also troubles with this exercise. The sense of this lesson is to get familiar with threads. But I think Luk is right and this exercise is not solvable on Sololearn. If you want you can write a mail: info@sololearn.com I will also inform SL about it. Happy coding!
25th Mar 2021, 6:17 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Luk Denise Roßberg I solved it, I just add an try{ } catch( Exceptions e) { } and it works
26th Mar 2021, 9:15 AM
Zotta
Zotta - avatar
+ 1
If SoloLearn's server running your code has a multi-core CPU then each thread can get started on a different core and priority doesn't mean anything. Priority only works for threads on the same CPU core. That's why the order is random in your program.
25th Mar 2021, 2:56 PM
Luk
Luk - avatar
+ 1
The problem with that task is that you don't get the same result every time you run the program. If it doesn't pass the test cases the first time, then it will pass the test cases the second or the third time. I found out that setPriority and Thread.sleep are unreliable when you call more than one object after each other. It only worked correctly when you called one object at a time and not multiple objects in a row. Maybe it only works correctly if you have multiple cpu's in your pc when calling multiple objects after each other.
26th Mar 2021, 1:30 PM
Jan
Jan - avatar
0
Luk so what I shall change?
25th Mar 2021, 3:04 PM
Zotta
Zotta - avatar
0
Zotta These threads are too simple to make CPU busy, so priority is pointless. It only works when there are too many threads and CPU has to decide which one to take. Threads are asynchronous from their definition, so if you want to execute code in good order, just don't use threads. We use threads when we don't care about the order.
25th Mar 2021, 3:10 PM
Luk
Luk - avatar
0
Luk so why sololearn requires to use them in this code?
25th Mar 2021, 3:11 PM
Zotta
Zotta - avatar
0
I have no idea because I don't have access to this exercise (it's for pro users).
25th Mar 2021, 3:18 PM
Luk
Luk - avatar
0
class Main { public static void main(String[ ] args) { Name name = new Name(); //set priority name.setPriority(1); Welcome welcome = new Welcome(); //set priority welcome.setPriority(2); welcome.start(); name.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"); } }
5th Jun 2023, 1:52 PM
Oleksandra Zabolotska
Oleksandra Zabolotska - avatar
0
If anyone is still stuck here is the right answer class Main { public static void main(String[] args) { Welcome welcome = new Welcome(); Name name = new Name(); // Set higher priority for Welcome welcome.setPriority(Thread.MAX_PRIORITY); // Set lower priority for Name name.setPriority(Thread.MIN_PRIORITY); welcome.start(); name.start(); } } // extend the Thread class class Welcome extends Thread { @Override public void run() { System.out.println("Welcome!"); } } // extend the Thread class class Name extends Thread { @Override public void run() { System.out.println("Please enter your name"); } }
15th Sep 2023, 8:51 PM
Anna