Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

android - Thread synchronization Java

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

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) I draw blue pulses that reflects the mutes of microphone. This is done by independent View class.

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
  }
}

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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...