2016-07-05 3 views
0

クラスTeamを定義し、そのクラス内にrunnable interfacesを実装すると、 team1およびteam2によって終了する。しかし、runnableWorkerOneのように直接クラスに実装すると、タスクをプリントする行に移動します。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......"); 
    } 
} 

コンソール出力がある:あなたが

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

ラッチはTeamのプライベートメンバーでやるので

[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

をしたいと考えています。 WorkerOneクラスの場合と同じように、この初期設定を行うことを意図していたのですが、忘れてしまいました。あなたはそれがlatchフィールド上countDown()を呼び出すときTeamランナブルがNullPointerExceptionを投げる作る掲載さ

コードを実行します。メインスレッドは0にカウントされないので、そのCountDownLatchを永久に待ちます。

0

問題があり、そしてあなたがTeamの2つのインスタンスを作成しています、それぞれに独自のラッチが付いています。

私はあなたが物事をこのようにやっている理由はわからないんだけど、私はあなたがTeamクラスのラッチ変数が初期化されることはありません

Team team = new Team(); 
    service.submit(team.team1); 
    service.submit(team.team2); 
関連する問題