- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 myCountDownLatch
(按预期工作):
public static void myCountDownLatch() {
CountDownLatch countDownLatch = new CountDownLatch(1);
Thread t = new Thread(() ->
{
try {
log.info("CountDownLatch: in thread..");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
t.start();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("CountDownLatch: out thread..");
}
我试图理解 CountdownLatch
和 ReentrantLock
的区别,并尝试使用 ReentrantLock
重写 myCountDownLatch
而不是 CountdownLatch
:
public static void myRentrantLock() {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
Thread t = new Thread(() ->
{
try {
log.info("ReentrantLock: in thread..");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
lock.lock();
t.start();
lock.unlock();
log.info("ReentrantLock: out thread..");
}
我只想在 Thread t
未完成时停止主线程,方法是使用 ReentrantLock
而不是 CountDownLatch
。
但是,myRentrantLock
的行为与我的 myCountDownLatch
不同。为什么?
最佳答案
您不能用 ReentrantLock
替换倒计时锁存器,ReentrantLock 是一种互斥和通知工具,但您可以使用 ReentrantLock
> 实现类似的功能。
它可能看起来像
public class MyLatch {
final ReentrantLock lock = new ReentrantLock();
final Condition zeroReached = lock.newCondition();
int remaining;
MyLatch(int count) {
if(count < 0) throw new IllegalArgumentException();
remaining = count;
}
public void await() throws InterruptedException {
lock.lock();
try {
while(remaining != 0) zeroReached.await();
}
finally {
lock.unlock();
}
}
public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
lock.lock();
try {
if(remaining == 0) return true;
long deadLine = System.nanoTime() + unit.toNanos(timeout);
while(remaining != 0) {
final long remainingTime = deadLine - System.nanoTime();
if(remainingTime <= 0) return false;
zeroReached.await(remainingTime, TimeUnit.NANOSECONDS);
}
return true;
}
finally {
lock.unlock();
}
}
public void countDown() {
lock.lock();
try {
if(remaining > 0 && --remaining == 0) zeroReached.signalAll();
}
finally {
lock.unlock();
}
}
public long getCount() {
lock.lock();
try {
return remaining;
}
finally {
lock.unlock();
}
}
}
ReentrantLock
保护内部状态,即 remaining
字段。关联的Condition ZeroReached
用于允许线程等待remaining
字段变为零。
它的使用方式与内置CountDownLatch
相同:
public class MyLatchTest {
public static void main(String[] args) {
int num = 10;
MyLatch countDownLatch = new MyLatch(num);
for(int i = 0; i < num; i++) {
Thread t = new Thread(() ->
{
try {
System.out.println("CountDownLatch: in thread..");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("CountDownLatch: one thread finished..");
countDownLatch.countDown();
});
t.start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("CountDownLatch: out thread..");
}
}
请注意,这里不需要显式的Lock
,Java 的内在锁定功能也可以工作:
public class MyLatch {
int remaining;
MyLatch(int count) {
if(count < 0) throw new IllegalArgumentException();
remaining = count;
}
public synchronized void await() throws InterruptedException {
while(remaining != 0) wait();
}
public synchronized boolean await(long timeout, TimeUnit unit) throws InterruptedException {
if(remaining == 0) return true;
long deadLine = System.nanoTime() + unit.toNanos(timeout);
while(remaining != 0) {
long remainingTime = deadLine - System.nanoTime();
if(remainingTime <= 0) return false;
wait(remainingTime / 1_000_000, (int)(remainingTime % 1_000_000));
}
return true;
}
public synchronized void countDown() {
if(remaining > 0 && --remaining == 0) notifyAll();
}
public synchronized long getCount() {
return remaining;
}
}
但无论哪种情况,内置的 CountDownLatch
都更高效......
关于java - ReentrantLock 替代 CountdownLatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59661898/
介绍 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 类的作用呢? 怎么说呢? 简
我是一名优秀的程序员,十分优秀!