+ 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"); } }
10 Answers
+ 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!
+ 2
Luk Denise RoĂberg I solved it, I just add an
try{
} catch( Exceptions e) {
}
and it works
+ 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.
+ 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.
0
Luk so what I shall change?
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.
0
Luk so why sololearn requires to use them in this code?
0
I have no idea because I don't have access to this exercise (it's for pro users).
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");
}
}
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");
}
}