Countdownlatch get count returns 个不一致的值
Countdownlatch get count returns inconsistent values
我已经开发了一个示例 java 程序来理解倒计时闩锁并用计数 4 初始化倒计时闩锁。我预计在 countDown 方法之后,getCount() 将 return 倒计时闩锁的剩余计数.但是,在下面的例子中:-
public static void main(String args[]) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(4);
Worker first = new Worker(latch, "WORKER-1");
Worker second = new Worker(latch, "WORKER-2");
Worker third = new Worker(latch, "WORKER-3");
Worker fourth = new Worker(latch, "WORKER-4");
first.start();
second.start();
third.start();
fourth.start();
latch.await();
System.out.println("Final Count:- " + latch.getCount());
}
}
class Worker extends Thread {
private CountDownLatch latch;
public Worker(CountDownLatch latch, String name) {
super(name);
this.latch = latch;
}
@Override
public void run() {
latch.countDown();
System.out.println("Count:- " + latch.getCount());
System.out.println(Thread.currentThread().getName() + " finished");
}
}
输出为:-
计数:- 2
计数:- 1
计数:- 2
WORKER-3 完成
WORKER-1 完成
WORKER-2 完成
计数:- 0
最终计数:- 0
WORKER-4 完成。
计数在输出中两次被 returned 为 2。我的代码有什么问题吗?
没问题。
How is CountDownLatch used in Java Multithreading?
此方法只进行 volatile
读取,而不进行 synchronised
读取。
/**
* Returns the current count.
*
* <p>This method is typically used for debugging and testing purposes.
*
* @return the current count
*/
public long getCount() {
return sync.getCount();
}
四个Worker
同时是运行。由于latch.countDown();
和latch.getCount()
不是原子的,执行顺序可能是:
thread1 latch.countDown();
thread2 latch.countDown();
thread1 System.out.println("Count:- " + latch.getCount()); // Count:- 2
thread2 System.out.println("Count:- " + latch.getCount()); // Count:- 2
我已经开发了一个示例 java 程序来理解倒计时闩锁并用计数 4 初始化倒计时闩锁。我预计在 countDown 方法之后,getCount() 将 return 倒计时闩锁的剩余计数.但是,在下面的例子中:-
public static void main(String args[]) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(4);
Worker first = new Worker(latch, "WORKER-1");
Worker second = new Worker(latch, "WORKER-2");
Worker third = new Worker(latch, "WORKER-3");
Worker fourth = new Worker(latch, "WORKER-4");
first.start();
second.start();
third.start();
fourth.start();
latch.await();
System.out.println("Final Count:- " + latch.getCount());
}
}
class Worker extends Thread {
private CountDownLatch latch;
public Worker(CountDownLatch latch, String name) {
super(name);
this.latch = latch;
}
@Override
public void run() {
latch.countDown();
System.out.println("Count:- " + latch.getCount());
System.out.println(Thread.currentThread().getName() + " finished");
}
}
输出为:-
计数:- 2
计数:- 1
计数:- 2
WORKER-3 完成
WORKER-1 完成
WORKER-2 完成
计数:- 0
最终计数:- 0
WORKER-4 完成。
计数在输出中两次被 returned 为 2。我的代码有什么问题吗?
没问题。 How is CountDownLatch used in Java Multithreading?
此方法只进行 volatile
读取,而不进行 synchronised
读取。
/**
* Returns the current count.
*
* <p>This method is typically used for debugging and testing purposes.
*
* @return the current count
*/
public long getCount() {
return sync.getCount();
}
四个Worker
同时是运行。由于latch.countDown();
和latch.getCount()
不是原子的,执行顺序可能是:
thread1 latch.countDown();
thread2 latch.countDown();
thread1 System.out.println("Count:- " + latch.getCount()); // Count:- 2
thread2 System.out.println("Count:- " + latch.getCount()); // Count:- 2