+ 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.
9 Answers
+ 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!");
}
}
+ 1
Brian i testet both but I didn't got stable results.
+ 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; }
}
+ 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");
}
}
0
First start the threads then set the priority.
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.
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);
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();
- 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