本文介绍了线程同步的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的Andr​​oid应用程序,无法同步查看与硬件。让我来解释一下。

I am working on Android App and unable to synchronize View with Hardware.Let me explain.

1)我静音和Android的基础上随机值(这是随机的休眠)存放在数组A,从内螺纹1的run方法取消静音麦克风。

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.

2)我画蓝色脉冲反映麦克风的静音。这是由独立的视图类来完成。

2) I draw blue pulses that reflects the mutes of microphone. This is done by independent View class.

3)我将跨越上述观点得出图中的红线,通过从一个倒数计时器的onTick调用。

3)I move a red line across the graph drawn in above view, by calling from within onTick of a countdown timer.

我开始两个线程一前一后等,这种方式:

I start the two threads one after other, this way:

Thread1.start

Thread1.start

counter.start();

counter.start();

如何同步这两个,我想要做的三件事情在时间,避免多个线程。三件事情是:绘制脉冲(这是恒定),使得横过x轴的红线移动并尽快触摸蓝色脉冲作为电话静音,保持移动每一秒,脉冲宽度反映了延迟的持续时间。只要话筒即将解除静音,红线应该离开的脉搏,继续前进。

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.

目前,code是做什么我想要的。但没有synchroization。无论是麦克风,它的工作第一,或图形动作快。它们不是在同步。

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.

有没有办法来保存一个线程,迫使它表现为coutdowntimer或同步两者。我不能嵌入红线异动,线程1,因为,它将具有横过x轴的进步每秒

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.

推荐答案

这听起来像你将需要使用ReentrantLock"和一个Condition"

It sounds like you are going to need to use a "ReentrantLock" and a "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();
  }
}

行condition.await()中的threadTwoMethod会导致threadTwoMethod暂停,直到threadOneMethod称为condition.singalAll()。在呼叫信号,或等待,在一个条件,你必须拥有的条件是由创造了锁。这就是为什么我们有lock.lock()/ 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.

来电等待()应放置在一个while循环,因为你的线程会随机唤醒,即使它正在等待病情一直没有信号。循环是通过使用布尔标志来实现在本实施例

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.

记住要锁定/解锁的尝试和finally块。如果抛出一个异常,你会想要确保你仍然解锁锁,这就是为什么我们把解锁finally块。

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.

您也可以使用LinkedBlockQueue和吃来完成在不太混乱的方式类似。如果我有更多的时间我会更明确,但我希望这会有所帮助。

You could also use a LinkedBlockQueue and "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.

这篇关于线程同步的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:15
查看更多