如何更改此程序以创建50个线程?我的代码只创建了47个线程。 [英] How do I change this program to create 50 threads? My code creates only 47 threads.

查看:130
本文介绍了如何更改此程序以创建50个线程?我的代码只创建了47个线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

问题是 -

编写一个名为ReverseHello.java的程序,创建一个线程(让我们称之为线程1)。线程1创建另一个线程(线程2);线程2创建线程3;等等,直到线程50.每个线程都应该打印Hello from Thread< num>!,但你应该构建程序,使线程以相反的顺序打印问候语。

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.







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();
	}
}





我的尝试:



我尝试创建大小为53且从52运行到0的数组,但输出相同。它只创建了47个线程。



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.

推荐答案

由于你的主要问题是有47个线程而不是50个并且你没有注意到2500个'Hello'消息而不是50个,我怀疑问题是你的计数方法。



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:

问题是 -

编写一个名为ReverseHello.java的程序创建一个线程(让我们称之为线程1)。线程1创建另一个线程(线程2);线程2创建线程3;每个线程都应该打印Hello from Thread!,但你应该构建你的程序,使得线程以相反的顺序打印它们的问候语。

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.


要考虑的一些逻辑:

1.线程对象需要给出一个数字来告诉它它的序列号是什么是。

2.如果序列号小于51,那么:

2.1。它应该创建一个序列号为+1的新子线程。

2.2。然后它应该启动子线程并等待它完成。

3.它应该打印带有序列号的Hello消息。

4.它应该然后返回。



类的主要方法应该创建序列号为1的第一个线程,然后启动它 - 无需等待它完成。
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.


这是我的快速解决方案。可以进行改进。

This is my very quick solution. There are improvements to be made.
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();
 }
 
}


这篇关于如何更改此程序以创建50个线程?我的代码只创建了47个线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆