我正在做一个大学作业(坦率地说)。问题是我应该有4个客户端线程随时运行(直到数字n)。所以,当任何线程终止时,必须产生一个新的线程。让主线等待事件
public static void main(String[] args) throws IOException,InterruptedException
{
/* some declarations.. */
ClientThread client=new ClientThread();
Runnable intr =client;
for(count=1;count<=number;count++)
{
/* If 4 client threads has been spawned, wait until 1 of them exits */
while(Thread.activeCount()<5)
;
new Thread(intr).start();
}
/* Wait until all other threads exits. */
while(Thread.activeCount()!=1)
;
System.out.println("\n The sum of factorials is: "+client.getResult());
}
我想删除忙等待,因为它击败了我的程序的目的。我怎样才能使主线程wait
?? (它显示wait
是一种非静态方法,不能从静态方法调用。)请帮助。
除非您已经计算了阶乘多线程,否则使用多线程可能会比使用多线程慢。 – 2011-01-24 09:40:09
我得计算'n'数的阶乘之和:1!+ 2!+ .. + n!并且每个阶乘计算都是独立的(通过服务器的不同线程)。它是否仍然较慢?我不知道,只是实施我的教授告诉的。 : -/ – letsc 2011-01-24 09:43:57