java 线程同步Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread synchronization Java
提问by David Prun
I am working on Android App and unable to synchronize View with Hardware. Let me explain.
我正在使用 Android 应用程序,但无法将视图与硬件同步。让我解释。
1) I mute and unmute microphone of Android based on random values (which are random sleeps) stored in array A, from within run method of Thread 1.
1)我根据存储在数组 A 中的随机值(随机睡眠)从线程 1 的 run 方法中将 Android 的麦克风静音和取消静音。
2) I draw blue pulses that reflects the mutes of microphone. This is done by independent View class.
2)我绘制了反映麦克风静音的蓝色脉冲。这是由独立的 View 类完成的。
3)I move a red line across the graph drawn in above view, by calling from within onTick of a countdown timer.
3)我通过从倒数计时器的 onTick 内调用,在上面视图中绘制的图形上移动一条红线。
I start the two threads one after other, this way:
我一个接一个地启动两个线程,这样:
Thread1.start
线程1.start
counter.start();
计数器。开始();
How to synchronize both of these, I want to do three things at a time and avoid multiple threads. Three things are: Draw the pulses (which is constant) , make the red line move across x axis and touch the blue pulse as soon as the phone is muted, and keep moving every second, the width of pulse reflects duration of delay. as soon as microphone is about to be unmuted, red line should leave the pulse and move forward.
如何同步这两个,我想一次做三件事,避免多线程。三件事是:绘制脉冲(恒定),使红线移动到x轴并在手机静音后立即触摸蓝色脉冲,并每秒保持移动,脉冲宽度反映延迟持续时间。一旦麦克风即将取消静音,红线应离开脉冲并向前移动。
Currently, code is doing what I want. but there is no synchroization. Either microphone does its job first, or graph moves fast. They are not in Sync.
目前,代码正在做我想要的。但没有同步。要么麦克风先完成它的工作,要么图形快速移动。它们不同步。
Is there a way to hold a thread, force it to behave as coutdowntimer or sync both of them. I cannot embed the red line movement in thread 1 because, it will have to progress across x axis every second.
有没有办法保持一个线程,强制它表现为 coutdowntimer 或同步它们。我不能在线程 1 中嵌入红线运动,因为它必须每秒穿过 x 轴。
回答by Matt Crinklaw-Vogt
It sounds like you are going to need to use a "ReentrantLock" and a "Condition"
听起来您将需要使用“ ReentrantLock”和“ Condition”
You can make one thread "wait" for another using the Condition created from a Reentrant lock:
您可以使用从重入锁创建的条件让一个线程“等待”另一个线程:
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private boolean someFlag = false;
public void threadOneMethod() {
lock.lock();
try {
someFlag = true;
condition.signalAll();
} finally {
lock.unlock();
}
}
public void threadTwoMethod() {
lock.lock();
try {
while (someFlag == false) {
condition.await();
}
System.out.println("Did some stuff");
someFlag = false;
} finally {
lock.unlock();
}
}
The line "condition.await()" in threadTwoMethod will cause threadTwoMethod to pause until threadOneMethod calls "condition.singalAll()". Before calling signal, or await, on a condition you must own the lock that the condition was created from which is why we have the "lock.lock() / lock.unlock()" calls.
threadTwoMethod 中的“condition.await()”行将导致 threadTwoMethod 暂停,直到 threadOneMethod 调用“condition.singalAll()”。在调用信号或等待之前,在条件下您必须拥有创建条件的锁,这就是为什么我们有“lock.lock() / lock.unlock()”调用的原因。
calls to await() should be placed in a while loop because your thread could be randomly woken up, even if the condition on which it is waiting hasn't been signaled. The loop is accomplished in this example by using a boolean flag.
对 await() 的调用应该放在 while 循环中,因为您的线程可能会被随机唤醒,即使它正在等待的条件尚未发出信号。在本例中,循环是通过使用布尔标志来完成的。
Remember to lock/unlock in try and finally blocks. If you throw an exception you will want to make sure you still unlock your lock, which is why we put unlock in a finally block.
记住在 try 和 finally 块中锁定/解锁。如果你抛出一个异常,你需要确保你仍然解锁你的锁,这就是我们将解锁放在 finally 块中的原因。
You could also use a LinkedBlockQueueand "take" to accomplish something similar in a less confusing way. If I had more time I would be more explicit, but I hope this helps.
您还可以使用LinkedBlockQueue和“采取”以一种不太容易混淆的方式完成类似的事情。如果我有更多时间,我会更明确,但我希望这会有所帮助。

