Threads | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Nov 2020, 1:41 PM
Roderick Davis
Roderick Davis - avatar
11 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
28th Nov 2020, 2:28 PM
Jayakrishna 🇮🇳
+ 5
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"); } }
31st May 2021, 5:56 AM
Abhishek Biswas
Abhishek Biswas - avatar
+ 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"); } }
4th Sep 2021, 6:29 PM
Fazal Haroon
Fazal Haroon - avatar
+ 1
Jayakrishna🇮🇳 this is a soloLearn problem then. Thanks.
28th Nov 2020, 2:42 PM
Roderick Davis
Roderick Davis - avatar
+ 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!"); } }
3rd Dec 2022, 9:06 AM
Mrunalini Vinayak Chaudhary
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.
28th Nov 2020, 2:24 PM
Roderick Davis
Roderick Davis - avatar
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
13th Aug 2022, 6:05 AM
Zoe Piper
Zoe Piper - avatar
0
عع
13th Feb 2024, 12:01 AM
Mohammed Rika
Mohammed Rika - avatar
- 1
What's your correct order of thinking? Can you elaborate it more? Edit : Roderick Davis am getting output : please enter your name Welcome
28th Nov 2020, 2:19 PM
Jayakrishna 🇮🇳
25th Jan 2021, 4:57 PM
David Ordás
David Ordás - avatar
- 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"); } }
27th Apr 2021, 8:50 AM
Nazariy Kupchenko
Nazariy Kupchenko - avatar