+ 1

59.2 Practice - Threads (Welcome!)

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. *Use setPriority(number) method on the objects - obj.setPriority(number). The higher the number, the higher the priority.

2nd Nov 2021, 11:51 PM
Chin Eu
Chin Eu - avatar
9 Antworten
+ 2
class Main { public static void main(String[ ] args) { Name name = new Name(); //establish priority name.setPriority(10); Welcome welcome = new Welcome(); //establish priority welcome.setPriority(1); name.start(); welcome.start(); } } //extend the class Thread class Welcome extends Thread{ public void run() { System.out.println("Please enter your name"); } } //extend the class Thread class Name extends Thread{ public void run() { System.out.println("Welcome!"); } }
16th Sep 2022, 4:56 AM
Monique Ollada
Monique Ollada - avatar
+ 1
Brian i testet both but I didn't got stable results.
3rd Nov 2021, 6:50 PM
Stefanoo
Stefanoo - avatar
+ 1
Not sure if this has changed, but as of writing this in 2023 the problem is called “Whats my account balance”. I was stuck on this for awhile so sharing what I learned. First I set the AccNumber and Balance values: //complete the constructor public User(string accNumber, double balance) { this.AccNumber=accNumber; this.Balance=balance; } The issue with the code is that the Balance property in the Account class is declared as private, which means it can only be accessed within the same class. Since the User class inherits from Account, it doesn’t have direct access to the Balance property, resulting in the error message “Account.Balance is inaccessible due to its protection level.” To fix this issue, you can change the access modifier of the Balance property in the Account class to be protected instead of private. This will allow derived classes, such as User, to access the Balance property. Here’s the modified code: class Account { protected double Balance { get; set; } }
11th Jun 2023, 2:36 PM
Forrest
Forrest - avatar
+ 1
Here is the correct code working. First, we need to start the thread, then set priority for that thread. public class Main { public static void main(String[ ] args) { Name name = new Name(); //set priority Welcome welcome = new Welcome(); //set priority welcome.start(); welcome.setPriority(10); name.start(); name.setPriority(1); } } //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"); } }
17th Jul 2023, 1:52 AM
sudhir meena
sudhir meena - avatar
0
First start the threads then set the priority.
3rd Nov 2021, 12:32 AM
Stefanoo
Stefanoo - avatar
0
Okey it doesn't work every time and i found this. https://stackoverflow.com/questions/44617921/java-multithreading-setpriority/44618364#44618364 I think handle it by yourself is the way to go. You could work with booleans and set them on true if one thread is finished, but i think then the idea of threads is broken.
3rd Nov 2021, 2:26 AM
Stefanoo
Stefanoo - avatar
0
Chin Eu the problem description makes it clear that "the higher the number, the higher the priority." You have priority reversed. Change name.setPriority(10); welcome.setPriority(1); To name.setPriority(1); welcome.setPriority(10);
3rd Nov 2021, 4:09 PM
Brian
Brian - avatar
0
This is what I learned from this problem: (Quoting Philip Ten's response to a question on StackExchange): "Thread priority does not guarantee execution order. It comes into play when resources are limited. If the System is running into constraints due to memory or CPU, then the higher priority threads will run first. Assuming that you have sufficient system resources then you will not have any system constraints." https://stackoverflow.com/questions/12038592/java-thread-priority-has-no-effect //use the order of the two following lines to guarantee correct result. name.start(); welcome.start();
29th Aug 2022, 3:53 PM
Stephan Peters
Stephan Peters - avatar
- 1
My code: class Main { public static void main(String[ ] args) { Name name = new Name(); //establish priority name.setPriority(10); Welcome welcome = new Welcome(); //establish priority welcome.setPriority(1); name.start(); welcome.start(); } } //extend the class Thread class Welcome extends Thread{ public void run() { System.out.println("Welcome!"); } } //extend the class Thread class Name extends Thread{ public void run() { System.out.println("Please enter your name"); } } The answer comes out as: Please enter your name Welcome! Intead of the expected answer: Welcome! Please enter your name
2nd Nov 2021, 11:55 PM
Chin Eu
Chin Eu - avatar