- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道我在这里做错了什么。 Countdownlatch.await 不起作用。绝对没有等待时间。我用荒谬的数字(50 秒)进行了测试,并立即收到了调用后记。我正在尝试在 UI 线程上使用它。我还有另一个线程在运行。
代码:
private final CountDownLatch latch = new CountDownLatch(1); //called earlier
void someMethod() {
//code
Runnable flashScreen = new Runnable() {
@Override
public void run() {
flashScreen();
}
};
runOnUiThread(flashScreen);
//Mode code
}
private void flashScreen()
{
final TextView monthDayYear = (TextView)findViewById(R.id.month_day_year);
final TextView hourMinuteSecond = (TextView)findViewById(R.id.hour_minute_second);
//Get date and time
final SyncModule syncModule = _application.getSyncModule();
syncModule.start();
while(counter.intValue() < 150) {
try {
//Thread.sleep(10);
latch.countDown();
latch.await(10, TimeUnit.MILLISECONDS);
counter.incrementAndGet();
} catch (InterruptedException ex) {
Util.appendLog("SyncActivity: InterruptedException while flashing camera " + ex.getMessage());
}
}
final ImageView fadeView = (ImageView)findViewById(R.id.sync_flash_rectangle);
fadeView.setBackgroundColor(Color.WHITE);
fadeIn(fadeView, 1, 0, 1000, true);
Calendar syncTime = syncModule.getDateAndTime();
monthDayYear.setText(new SimpleDateFormat("MM/dd/yyyy").format(syncTime.getTime()));
hourMinuteSecond.setText(new SimpleDateFormat("HH:mm:ss:00").format(syncTime.getTime()));
try {
latch.countDown();
latch.await(50, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
Util.appendLog("SyncActivity: InterruptedException while flashing camera " + ex.getMessage());
}
counter.set(1000);
}
最佳答案
我不确定您是否了解 CountDownLatch
的工作原理。 countDown()
方法和await()
方法一般在不同的线程中调用。
来自 CountDownLatch
的 Java8 JavaDoc :
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
在您当前的代码中,您的闩锁倒计时为 1(从示例的第一行 new CountDownLatch(1);
)。在您调用 await()
的两个位置,您紧接在调用 countDown()
之前,这会导致您的闩锁倒计时达到 0(或什至更少) .因此,任何对 await()
的调用都不会阻塞,因为倒计时已经到了。
我不是很清楚你想要完成什么,但如果你提供更多细节,我可能会提供进一步的建议。
关于java - 安卓。 Countdownlatch.await 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24170595/
介绍 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 类的作用呢? 怎么说呢? 简
我是一名优秀的程序员,十分优秀!