Answer to Question #249481 in Java | JSP | JSF for Locos

Question #249481

Write a program called ReverseHello.java that creates a thread (let's call it

Thread 1). Thread 1 creates another thread (Thread 2); Thread 2 creates

Thread 3; and so on, up to Thread 50. Each thread should print "Hello from

Thread <num>!", but you should structure your program such that the

threads print their greetings in reverse order.


1
Expert's answer
2021-10-12T05:14:22-0400


package hellothread;


public class HelloThread extends Thread {
private int count;
	public HelloThread(int n){
		count =n;
	}
	public void run()
	{
		if(count<50)
		{
			createThread(count+1);	
		}
		System.out.println("hello " + count);	
	}
	public void createThread(int counter) {
		HelloThread th=new HelloThread(counter);
		th.start();		
		try {
			th.join();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}				
	}
	public static void main(String[] arg){		
		HelloThread th = new HelloThread(1);
		th.start();		
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS