- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道我可以直接使用 CountDownLatch
,但是,作为练习并为了更好地理解 Phaser
,我想使用它而不是 COuntDownLatch
。
因此,我将创建 N 个等待者,以及一个需要翻转锁存器的线程。所有等待者,如果在翻转之前到达,都会阻塞,但是在闩锁倒计时之后,所有后续的 await()
立即返回。
WIth Phaser
我不知道如何实现这一点...障碍很容易,因为我们有 N + 1 个线程,每个线程都到达并等待。然而,为了确保没有线程在第一阶段之后等待,不知何故,我感到困惑。
我能想到的唯一方法如下:
Phaser p = new Phaser(1);
int phase = p.getPhase();
....
// thread that awaits()
new Thread(new Runnable() {
....
p.awaitAdvance(phase)
}
另一个线程只是将移相器推进到下一阶段。这并不理想,因此我们将不胜感激。
最佳答案
我不同意@tiago-cogumbreiro 的回答。事实上,Phaser
可以用来模拟CountDownLatch
的行为。
您可以使用 Phaser 的 arrive()
方法来实现它。它不会等待其他线程到达。因此,可以实现 CountDownLatch
行为。
Phaser 的 arriveAndAwaitAdvance()
使调用它的线程等待,直到其他线程/方到达。因此,该方法可用于模拟 CyclicBarrier
的行为。
下面是使用 Phaser
模拟 CountDownLatch(3)
行为的源代码。如果只使用一个WorkerThread
,可以模拟CountDownLatch(1)
如果将 arrive()
方法替换为 arriveAndAwaitAdvance()
方法,下面的代码将模拟 CyclicBarrier
。
public class PhaserAsCountDownLatch {
static Phaser phaser = new Phaser(3);
public static void main(String[] args) {
new WorkerThread2().start();
new WorkerThread3().start();
new WorkerThread().start();
//waits till phase 0 is completed and phase is advanced to next.
phaser.awaitAdvance(0);
System.out.println("\nFROM MAIN THREAD: PHASE 0 COMPLETED");
System.out.println("FROM MAIN THREAD: PHASE ADVANCED TO 1");
System.out.println("MAIN THREAD ENDS HERE\n");
}
static class WorkerThread extends Thread{
@Override
public void run() {
for (int i = 1; i < 5; i++) {
System.out.println("tid: " + Thread.currentThread().getId() + ", BEFORE ARRIVING val is: " + i);
}
phaser.arrive();
System.out.println("ARRIVED tid: " + Thread.currentThread().getId());
}
}
static class WorkerThread2 extends Thread{
@Override
public void run() {
//won't wait for other threads to arrive. Hence, CountDownLatch behaviour can be achieved
phaser.arrive();
System.out.println("ARRIVED tid: " + Thread.currentThread().getId());
for (int i = 200; i < 231; i++) {
System.out.println("tid: " + Thread.currentThread().getId() + " AFTER ARRIVING. val is: " + i);
}
}
}
static class WorkerThread3 extends Thread{
@Override
public void run() {
//won't wait for other threads to arrive. Hence, CountDownLatch behaviour can be achieved
phaser.arrive();
System.out.println("ARRIVED tid: " + Thread.currentThread().getId());
for (int i = 300; i < 331; i++) {
System.out.println("tid: " + Thread.currentThread().getId() + " AFTER ARRIVING. val is: " + i);
}
}
}
}
关于java - Phaser - 如何将其用作 CountDownLatch(1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45600243/
介绍 CountDownLatch是一个同步辅助类,它允许一个或多个线程一直等待直到其他线程执行完毕才开始执行。 用给定的计数初始化CountDownLatch,其含义是要被等待执行完的线程个数。 每
在多线程开发中,常常遇到希望一组线程完成之后在执行之后的操作,java提供了一个多线程同步辅助类,可以完成此类需求: 类中常见的方法: 其中构造方法: CountDownLatch(int
一 点睛 1 定义 CountDownLatch 是一个同步工具类,可以用来协调多个线程的执行时间。例如,可以让 A 线程在其他线程运行完毕后再执行。也就是说,如果其他线程没有执行完毕,则 A 线程就
以下情况:线程 A 启动线程 B,并应等待线程 B 完成其工作。线程 B 可以启动一个新的线程 C。如果是这样,线程 A 应该等待线程 B 和线程 C。 我可以使用两个 CountDownLatch
有没有办法多次使用唯一的CountDownLatch? 我的意思是在创建 CountDownLatch 实例并设置其计数值后,通过调用该实例上的 countDown() 方法达到 0 后,我们设置一个
大家好,我正在学习教程,我对我尝试应用从教程中获得的知识的一些事情感到好奇。 下面是一个Runner类 package Tutorial2; import java.util.concurrent.C
我有一个希望打印值的代码示例。正如我在 countDownLatch.countDown(); 之后的 run 方法中所想的那样被称为 CountDownLatch 应该达到零并且 main 方法应该
运行每个线程时,为什么即使前一个线程已经调用了 countdown.countDown() 并将 Latch Count 减少了 1,countdown.getCount() 仍总是打印“3”? 我有
我正在处理 CountDownLatch,我需要发送两个 JSON 以按顺序发送。 我想出了这个 CountDownLatch countDownLatch = new CountDownLat
我有一个方法 getNewA() 应该会阻塞,直到其他线程调用 setA(x)。使用 CountDownLatch 是否正确?我注意到存在数据竞争,因为在 gate.await() 解除阻塞后,另一个
有没有办法显式释放 CountDownLatch - 意味着不用 countDown()。 例如:假设我正在等待 100 个线程来执行 countDown(),但是如果某件事失败了,我想释放这个闩锁而
等待/通知和锁定/条件等并发管理机制似乎受到spurious wakeups的影响.开发人员通过重新检查条件是否确实发生了变化来应对这些意外唤醒。 就 CountDownLatch 而言,虚假唤醒是一
对于某些并发编程,我可以使用 Java 的 CountDownLatch概念。是否有 C++11 的等效项,或者该概念在 C++ 中称为什么? 我想要的是在计数达到零时调用一个函数。 如果还没有,我会
我有一个方法,它从 Firebase 存储加载图像。它在后台线程中调用,我需要阻止它,直到加载图像(以避免回调 hell )。这是代码(在 Kotlin 中) override fun fromNet
我正在使用 CountDownLatch在两个线程之间同步初始化过程,我想知道如何正确处理 InterruptedException它可能会抛出。 我最初写的代码是这样的: private C
使用有什么好处 java.util.concurrent.CountdownLatch 而不是 java.util.concurrent.Semaphore ? 据我所知,以下片段几乎是等价的: 1.
关注 王有志 ,一个分享硬核Java技术的互金摸鱼侠 欢迎你加入 Java人的提桶跑路群 : 共同富裕的Java人 今天我们来聊一聊AQS家族中的另一个重要成员Coun
功能简介 闭锁是一种同步工具类,可以延迟线程的进度直到其到达终止状态【CPJ 3.4.2】。闭锁的作用相当于一扇门∶ 在闭锁到达结束状态之前,这扇门一直是关闭的,并且没有任何线程能通过,当到达
一入王者深似海,从此对象是路人。 哈喽观众老爷们你们好,在下战神吕布字奉先,今天给大家来一部吕布的教学视频! 咳咳,不对。大家好,我是磊哥,今天给大家来一篇 CountDownLatch 的
本章节我们来讨论下 java.util.concurrent.CountDownLatch 这个类,顺带演示下如何在一些实际例子中使用它。 CountDownLatch 类的作用呢? 怎么说呢? 简
我是一名优秀的程序员,十分优秀!