2011-01-24 125 views
1

我正在做一个大学作业(坦率地说)。问题是我应该有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是一种非静态方法,不能从静态方法调用。)请帮助。

+0

除非您已经计算了阶乘多线程,否则使用多线程可能会比使用多线程慢。 – 2011-01-24 09:40:09

+0

我得计算'n'数的阶乘之和:1!+ 2!+ .. + n!并且每个阶乘计算都是独立的(通过服务器的不同线程)。它是否仍然较慢?我不知道,只是实施我的教授告诉的。 : -/ – letsc 2011-01-24 09:43:57

回答

5

java.util.concurrent。 CountDownLatch是专为您的情况。

定义CountDownLatch doneSignal = new CountDownLatch(4);

doneSignal.await()会等到doneSignal.countDown()被称为四次。 因此,让ClientThreads保留相同参考doneSignal,当run()退出时,请致电doneSignal.countDown()

class ClientThread implements Runnable { 
    private final CountDownLatch doneSignal; 
    ClientThread (CountDownLatch doneSignal) { 
     this.doneSignal = doneSignal; 
    } 
    public void run() { 
     try { 
     //Do something 
     doneSignal.countDown(); 
     } catch (InterruptedException ex) {} 
    } 
} 
... 
//In main thread 
doneSignal.await(); 
+0

这实际上工作吗?它似乎等待4线程退出主线程之前退出,而我相信问题是客户端线程退出主线程应该立即产生另一个线程。 – 2011-06-30 04:53:18

-1

主线程可以使用:

Thread.getCurrent().join(); 

这将等待所有产生的线程就不行了,主线程终止之前。

5

嗯 - 你有通过做手工,或者,你的老师希望你发现Executors.newFixedThreadPool(4)

这正是具有四个工作线程的线程池可以完成的工作:不再有四个客户端并行运行,如果终止,free'd工作线程已准备好“获得新工作”。


这很简单:

public void test(int threads, int runnables) { 
    ExecutorsService pool = Executors.newFixedThreadPool(threads); 
    Runnable r = new Runnable() { 
    public void run() { 
     // calculate a factorial or do something else 
     System.out.println(Thread.currenThread()); 
    } 
    } 
    for (int i = 0; i < runnables; i++) 
    pool.execute(r); 
} 

runnables更大然后threads,你会从最多threads线程数是(重新)用于执行可运行的结果看。

1

您可以在主类中编写回调方法,该方法由退出的线程调用并生成一个新类。通过在主类中使用静态字段,您将跟踪生成的线程数,以获得与当前代码相同的效果。

它看起来在某种程度上是这样的:

class Main{ 

    private static ClientThread client; 
    private static Runnable intr; 
    private static int count; 

    public static void main(String[] args) 
    { 
     count = 10; //Or something else 
     runningThreads = 0; 
     client=new ClientThread(); 
     intr =client; 
     for(i=0;i<5;i++) 
      spawnThread(); 
    } 

    private static void spawnThread() 
    { 
     Thread newThread; 
     if(count>0) 
     { 
      newThread = new Thread(intr); 
      newThread.start(); 
      count--; 
     } 

     if(count==0) 
      System.out.println("\n The sum of factorials is: "+client.getResult()); 
    } 
0

java.util.concurrent中 看看@类具体 java.util.concurrent.CountDownLatch java.util.concurrent.locks.Condition中