无法确定为什么在 class 中以匿名 classes 实现多个可运行接口时 CountDownLatch 无法正常工作
Unable to determine why CountDownLatch is not functioning when multiple runnable interfaces are implemented in anonymous classes inside a class
如果我定义一个 class Team
并在 class 中实现两个 runnable interfaces
,我不会在程序中到达任务 [= =15=] 和 team2
结束。但是,如果我像在 WorkerOne
中一样直接在 class 中实现 runnable
,我将到达它打印任务的行 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
问题是因为你
service.submit(new Team().team1);
service.submit(new Team().team2);
latch 是 Team
的私有成员,您已经创建了 Team
的两个实例,每个都有自己的 latch。
我不确定你为什么要这样做,但我相信你想要
Team team = new Team();
service.submit(team.team1);
service.submit(team.team2);
Team
class' 闩锁变量从未初始化。我怀疑您打算像在 WorkerOne
class.
中那样执行此初始化,但忘记了
执行您发布的代码会使 Team
runnable 在 latch
字段上调用 countDown()
时抛出 NullPointerException
。主线程永远等待它的 CountDownLatch
因为它永远不会倒数到 0.
如果我定义一个 class Team
并在 class 中实现两个 runnable interfaces
,我不会在程序中到达任务 [= =15=] 和 team2
结束。但是,如果我像在 WorkerOne
中一样直接在 class 中实现 runnable
,我将到达它打印任务的行 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
问题是因为你
service.submit(new Team().team1);
service.submit(new Team().team2);
latch 是 Team
的私有成员,您已经创建了 Team
的两个实例,每个都有自己的 latch。
我不确定你为什么要这样做,但我相信你想要
Team team = new Team();
service.submit(team.team1);
service.submit(team.team2);
Team
class' 闩锁变量从未初始化。我怀疑您打算像在 WorkerOne
class.
执行您发布的代码会使 Team
runnable 在 latch
字段上调用 countDown()
时抛出 NullPointerException
。主线程永远等待它的 CountDownLatch
因为它永远不会倒数到 0.