0

如果我定义一个类Team并在该类中实现两个runnable interfaces,我不会在程序中到达任务结束的team1team2。但是,如果我runnable直接在类中实现WorkerOne,我会在它结束时打印任务的位置WorkerOne。我不明白为什么任务永远不会完成team1并且team2应用程序没有停止。我在下面的控制台输出中包含了代码。我会感激任何想法或想法。谢谢你。

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
4

2 回答 2

0

类的Team锁存器变量永远不会被初始化。我怀疑您打算但忘记了,就像您在WorkerOne课堂上所做的那样进行初始化。

在您发布代码时执行代码会使可运行对象在调用该字段时Team抛出一个。主线程永远等待它,因为它永远不会倒数到 0。NullPointerExceptioncountDown()latchCountDownLatch

于 2016-07-06T05:41:44.367 回答
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);
于 2016-07-05T23:52:35.653 回答