2015-09-02 49 views
0

在我的程序中CountDownLatch的await()方法會繼續阻塞程序,CountDownLatch就像它寫的那樣, 是倒數鎖存器,當count爲零時觸發三個Thread執行,並且證明三個時cdAnswer降爲0 線程已經執行,然後主線程執行,但在我的程序中,三個線程只完成了 這兩個主線程被執行了,誰可以幫助我,我會非常感激。 底部是我的程序。關於CountDownLatch await()方法的問題?

public class CountdownLatchTest { 
    public static void main(String[] args) { 
     ExecutorService service = Executors.newCachedThreadPool(); 
     final CountDownLatch cdOrder = new CountDownLatch(1); 
     final CountDownLatch cdAnswer = new CountDownLatch(3); 

     for (int i = 0; i < 3; i++) { 
      Runnable runnable = new Runnable() { 
       @Override 
       public void run() { 
        try { 
         System.out.println("Thread" + Thread.currentThread().getName() 
           + "Wait for the command"); 

         cdOrder.await(); 
         System.out.println("Thread" + Thread.currentThread().getName() 
           + "received command"); 
         Thread.sleep((long) (Math.random() * 10000)); 
         System.out.println("Thread" + Thread.currentThread().getName() 
           + "Feedback the results"); 
         cdAnswer.countDown(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         System.out.println("To complete the order"); 
         cdAnswer.countDown(); 

        } 
       } 
      }; 
      service.execute(runnable); 
     } 
     try { 
      Thread.sleep((long) (Math.random() * 10000)); 

      System.out.println("Thread" + Thread.currentThread().getName() 
        + "The upcoming orders"); 
      cdOrder.countDown(); 
      System.out.println("Thread" + Thread.currentThread().getName() 
        + "Sent the command, are waiting for the result"); 
      cdAnswer.await(); 
      System.out.println("Thread" + Thread.currentThread().getName() 
        + "Have received all the response. " 
        + "The results of this task has been completed"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     service.shutdown(); 

    } 
} 

問題補充: 這個結果不出我所料,我想這句話「已收到所有的響應。該任務的結果已完成'將在最後打印

圖片顯示了我的程序結果。 enter image description here

+0

你能刪除中文字母嗎? –

+0

我無法理解你到底有什麼問題。你能否詳細說明一下? –

+0

非常好,這個問題的標題中的問號。 – Tunaki

回答

2

這是因爲,如果你看到運行的可運行

  public void run() { 
       try { 
        System.out.println("Thread" 
          + Thread.currentThread().getName() 
          + "Wait for the command"); 

        cdOrder.await(); 
        System.out.println("Thread" 
          + Thread.currentThread().getName() 
          + "received command"); 
        Thread.sleep((long) (Math.random() * 10000)); 
        System.out.println("Thread" 
          + Thread.currentThread().getName() 
          + "Feedback the results"); 
        cdAnswer.countDown(); -- countdown on latch cdAnswer 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        System.out.println("To complete the order"); 
        cdAnswer.countDown(); -- countdown on latch cdAnswer 

       } 
      } 

cdAnswer.countDown()方法;在每個可運行的終端和catch子句之前調用兩次。 因此,三次線程倒計數被稱爲六次,結果主線程在3次倒計時後啓動。

刪除cdAnswer.countDown();只是在上面的聲明它按預期工作

+0

噢,謝謝你,我在IDE的最右側的cdAnswer.countDown()的第一個方法,我看不到 – wanghao

+0

@wanghao! :) – virendrao