I know that there are a lot of similar questions, but no one helped me. I am getting IllegalMonitorStateException: object not locked by thread before wait() when I try to pause the thread.
Here is my initialization method:
// called only once in constructor; the variables are global ( private Thread ... ) public void init() { recordingThread = new Thread(new Runnable() { @Override public void run() { isNewRecordingThread= false; record(); } }); recognitionThread = new Thread(new Runnable() { @Override public void run() { isNewRecognition= false; recognize(); } }); ... } startRecording method:
private synchronized void startRecording(Thread recordingThread) { if(isNewRecordingThread){ recordingThread.start(); return; } recordingThread.notify(); } startRecognition method:
private synchronized void startRecognition(Thread recognitionThread) { shouldContinueRecognition = true; if(isNewRecognition){ recognitionThread.start(); return; } recognitionThread.notify(); } And the stopping method where I actually get the error:
private synchronized void stopRecordingAndRecognition(Thread recordingThread, Thread recognitionThread) { try{ if (recordingThread != null && recordingThread.isAlive()) { recordingThread.wait(); } if (recognitionThread != null && recognitionThread.isAlive()) { recognitionThread.wait(); } } catch (InterruptedException e){ Log.d("TESTING","InterruptedException e= "+e); } }
wait()/await()without loop because you can miss a signal or have a spurious wake-up!