相关疑难解决方法(0)

使主线程等待事件

我正在做大学任务(坦率地说).问题是我应该在任何时候运行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());
   }
Run Code Online (Sandbox Code Playgroud)

我想删除忙碌的等待,因为它违背了我的程序的目的.我怎样才能制作主线程wait?(它显示的wait是非静态方法,不能从静态方法调用.)请帮忙.

java multithreading

1
推荐指数
2
解决办法
8073
查看次数

标签 统计

java ×1

multithreading ×1