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 ar
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.