2016-07-05 53 views
0

如果我在該類中定義了一個類Team並實現了兩個runnable interfaces,我不明白任務中的任務由team1team2結束。但是,如果我在WorkerOne中直接實現runnable,我會到達打印任務的地方,由WorkerOne結束。我不明白爲什麼team1team2的任務從未完成,並且應用程序沒有停止。我在下面包含了代碼和控制檯輸出。我會欣賞任何想法或想法。謝謝。無法確定在一個類中的匿名類中實現多個可運行接口時爲什麼CountDownLatch無法正常工作

import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

class WorkerOne implements Runnable { 
    private CountDownLatch latch; 

    public WorkerOne(CountDownLatch latch) { 
     this.latch = latch; 
    } 

    @Override 
    public void run() { 
     System.out 
       .println("[Tasks by WorkerOne : ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + " START"); 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     latch.countDown(); 
     System.out.println("[Tasks by WorkerOne : ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + " END"); 

    } 

} 

class Team { 
    private CountDownLatch latch; 

    Runnable team1 = new Runnable() { 
     public void run() { 
      System.out.println("[Tasks by team1: ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + "START"); 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      latch.countDown(); 
      System.out.println("[Tasks by team1 : ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + " END"); 

     } 
    }; 

    Runnable team2 = new Runnable() { 
     public void run() { 
      System.out.println("[Tasks by team2 : ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + "START"); 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      latch.countDown(); 
      System.out.println("[Tasks by team2 : ]" + " :: " + "[" + Thread.currentThread().getName() + "]" + " END"); 

     } 
    }; 
} 

public class Demo { 

    public static void main(String[] args) { 
     CountDownLatch latch = new CountDownLatch(3); 
     ExecutorService service = Executors.newFixedThreadPool(3); 
     service.submit(new WorkerOne(latch)); 
     service.submit(new Team().team1); 
     service.submit(new Team().team2); 
     try { 
      latch.await(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("Tasks completed......"); 
    } 
} 

控制檯輸出爲:

[Tasks by WorkerOne : ] :: [pool-1-thread-1] START 
[Tasks by team1: ] :: [pool-1-thread-2]START 
[Tasks by team2 : ] :: [pool-1-thread-3]START 
[Tasks by WorkerOne : ] :: [pool-1-thread-1] END 

回答

0

Team類的鎖變量從未被初始化。我懷疑你打算,但忘了,做這個初始化,就像你在WorkerOne類中做的那樣。

執行您發佈的代碼時,Team運行列表在latch字段上調用countDown()時會拋出NullPointerException。主線程在其CountDownLatch上永遠等待,因爲它永遠不會倒數爲0.

0

的問題是,因爲你做

service.submit(new Team().team1); 
    service.submit(new Team().team2); 

鎖存是Team私有成員,並且您已經創建的Team兩個實例,每個都有自己的閂鎖。

我不知道爲什麼你正在做的事情這樣,但我相信你想

Team team = new Team(); 
    service.submit(team.team1); 
    service.submit(team.team2); 
相關問題