Write a class called ReverseGreet that creates a thread say Thread1, Thread1 creates another thread say Thread2, Thread2 creates Thread3; and so on, up to a given number of input.
Each thread should print "CodeQuotient Thread<num>", but you should structure your program such that the threads print their greetings in reverse order. e.g.
Sample Input1
8
Sample Output1
CodeQuotient Thread8
CodeQuotient Thread7
CodeQuotient Thread6
CodeQuotient Thread5
CodeQuotient Thread4
CodeQuotient Thread3
CodeQuotient Thread2
CodeQuotient Thread1
Sample Input2
2
Sample Output2
CodeQuotient Thread2
CodeQuotient Thread1
//Updated Solution
import java.util.Scanner;
// Complete the class below as sopecified above.
class ReverseGreet extends Thread
{
int n;
public ReverseGreet(int n)
{
this.n=n;
}
public void run()
{
if(n>0)
{
System.out.println("CodeQuotient Thread"+n);
ReverseGreet newThread = new ReverseGreet(n-1);
newThread.start();
try
{
newThread.join();
}
catch(Exception e)
{
}
}
}
}
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ReverseGreet t1 = new ReverseGreet(n);
t1.start();
}
}
import java.util.Scanner;
class ReverseGreet extends Thread {
private int threadNumber;
// Constructor to initialize the thread number
public ReverseGreet(int threadNumber) {
this.threadNumber = threadNumber;
}
// Method to create and start threads recursively
@Override
public void run() {
for(int i=threadNumber;i>0;i--)
{
System.out.println("CodeQuotient Thread"+i);
}
}
}
class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // Get the number of threads from user input
ReverseGreet thread = new ReverseGreet(n);
thread.start(); // Start the first thread
}
}
x