+ 1
Threads
Why won’t this output in the right order? import java.lang.*; 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(10); 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"); } } https://code.sololearn.com/cn8jpBukX7H2/?ref=app
12 Answers
+ 1
For simple operations don't rely on threads.. It depends on os support.. Once look at this : hope it helps.. Roderick Davis
https://www.sololearn.com/Discuss/2211828/?ref=app
+ 6
make sure the thread priority number should be same and manipulate the function here i kept the priority number same as 5 for safe limits and i changes the function calling like in question name came first then welcome but i change it into welcome first and then the name function call and the program run smoothly.
import java.lang.*;
class Main {
public static void main(String[ ] args) {
Name name = new Name();
//set priority
name.setPriority(5);
Welcome welcome = new Welcome();
//set priority
welcome.setPriority(5);
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");
}
}
+ 3
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
class Main {
public static void main(String[ ] args) {
Name name = new Name();
name.setPriority(1);
Welcome welcome = new Welcome();
welcome.setPriority(2);
name.start();
welcome.start();
}
}
class Welcome extends Thread{
public void run() {
System.out.println("Welcome!");
}
}
class Name extends Thread{
public void run() {
System.out.println("Please enter your name");
}
}
+ 1
Jayakrishna🇮🇳 this is a soloLearn problem then. Thanks.
+ 1
class Main {
public static void main(String[ ] args) {
Name name = new Name();
name.setPriority(1);
Welcome welcome = new Welcome();
welcome.setPriority(2);
name.start();
welcome.start();
}
}
class Welcome extends Thread{
public void run() {
System.out.println("Please enter your name");
}
}
class Name extends Thread{
public void run() {
System.out.println("Welcome!");
}
}
0
Jayakrishna🇮🇳
The question:
We are writing a registration program for our app. At first it should welcome the users, then ask the users to enter their names.
But program you are given executes this sequence in reverse order.
Complete the program by extending the Thread class for Welcome and Name classes, then setting priorities for their run methods so that the program outputs the messages in the correct order.
0
I don’t know what’s wrong so I switched these statements
welcome.start();
name.start();
To get a good result.
This is not the way but
My code doesn’t work
0
عع
0
it's a Sololearn compiler error so, use it like this. this solution will only complete the task.
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(10);
//swap the place
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");
}
}
- 1
What's your correct order of thinking? Can you elaborate it more?
Edit : Roderick Davis am getting output :
please enter your name
Welcome
- 1
Jayakrishna🇮🇳 Same problem to me...
https://www.sololearn.com/Discuss/2672570/?ref=app
- 3
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(10);
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");
}
}