当在类

时间:2016-07-05 23:38:33

标签: java multithreading countdownlatch

如果我定义了一个类Team并在该类中实现了两个runnable interfaces,那么我无法通过team1team2来完成任务中的任务结束。但是,如果我在runnable中直接在类中实现WorkerOne,那么我会在WorkerOne结束时打印任务。我不明白为什么team1team2的任务永远不会完成,应用程序也没有停止。我已将代码与控制台输出一起包含在下面。我会感激任何想法或想法。谢谢。

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

2 个答案:

答案 0 :(得分:0)

问题是因为你做了

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

latch是Team的私有成员,你创建了Team的两个实例,每个实例都有自己的锁存器。

我不确定你为什么这样做,但我相信你想要

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

答案 1 :(得分:0)

Team班级' latch变量永远不会被初始化。我怀疑你有意,但忘记了,进行初始化,就像在WorkerOne课程中那样。

在发布代码时执行代码会使Team runnables在NullPointerException字段上调用countDown()时抛出latch。主线程在其CountDownLatch上永远等待,因为它永远不会倒数到0。