Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
The question is-
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.


Java
class reversehello extends Thread{
	public void run()
	{
		for(int j=49;j>=0;j--)
			System.out.println("Hello from Thread <"+(j+1)+">");
		try{
			Thread.sleep(1000);
		}
		catch(Exception e)
		{System.out.println(e);}
	}
	public static void main(){
		reversehello t[]=new reversehello [50];
		for(int i=0;i<50;i++)
			t[i]=new reversehello();
		for(int i=49;i>=0;i--)
			t[i].start();
	}
}


What I have tried:

I have tried creating array of size 53 and running from 52 to 0,but the output was same.It creates only 47 threads.
Posted
Updated 23-Mar-17 4:47am
v2
Comments
Philippe Mori 16-Aug-16 19:02pm    
Why would you have a loop in both run function and in main function?
Anurita Srivastava 17-Aug-16 14:54pm    
The loop in main is to start() the threads and in run is to print the output,to actually print the number of thread.
Mohibur Rashid 16-Aug-16 19:39pm    
How do you know that it creates only 47?
Anurita Srivastava 17-Aug-16 14:53pm    
because the output prints only till Hello from Thread 47.
Mohibur Rashid 17-Aug-16 19:59pm    
Your application runs all 50 threads + 1(main thread). But your logic is incorrect. Follow what Richard MacCutchan said

Since your main problem is having 47 threads instead of 50 and that you did not noticed 2500 'Hello' messages instead of 50, I suspect the problem is your method of counting.

Quote:
The question is-
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 !", but you should structure your program such that the threads print their greetings in reverse order.
Your code is not doing this at all, It is not a little different, it is completely different. A complete rewrite is in order.
 
Share this answer
 
Some logic to think about:
1. The thread object needs to be given a number to tell it what sequence number it is.
2. If the sequence number is less than 51 then:
2.1. It should create a new child thread with the sequence number + 1.
2.2. It should then start the child thread and wait for it to finish.
3. It should print the "Hello" message with its sequence number.
4. It should then return.

The main method of the class should create the first thread with sequence number 1, and then start it - no need to wait for it to finish.
 
Share this answer
 
v4
Comments
Anurita Srivastava 17-Aug-16 15:01pm    
How do I give a number to my thread?Can you please explain in details?
Richard MacCutchan 18-Aug-16 3:19am    
Add an integer field to your reversehello class, and a constructor that takes an integer value. The constructor can then save it, and the code in your run method can check it.
Anurita Srivastava 28-Aug-16 10:49am    
oh,thanks :)
This is my very quick solution. There are improvements to be made.
C#
class reverseGreeting extends Thread
{
  private int counter;
 public reverseGreeting(int counter)
 {
   super("Thread" + counter);
   this.counter = counter;
 }
  
 public void run()
 {
    counter++ ;
   if(counter <= 51)
   {
   reverseGreeting thr = new reverseGreeting(counter);
  thr.start();
  try {
    thr.join();
     System.out.println("Hello from "+ getName()); 
  }
  catch (Exception e)
  {
   //do smth
  }
   }
 } 

 public static void main (String args[])
 {
   reverseGreeting th1 = new reverseGreeting(1);
   th1.start();
 }
 
}
 
Share this answer
 
class App extends Thread{
	int counter;
	public App(int counter){
		this.counter=counter;
	}
	public void run()
	{
		if(counter<50)
		{
			createThread(counter+1);	
		}
		System.out.println("hello" + counter);	
	}
	public void createThread(int counter) {
		App t=new App(counter);
		t.start();		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				
	}
	public static void main(String[] arg){		
		App t = new App(1);
		t.start();		
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900