Hi,
I have two classes running on different threads and both use the same resource.
"isUpdateNeeded" is the var that I want to update (the lock). The method "setUpdateVar()" change the value of this variable to true and notify the
thread "UpdateVar" to use this variable. The issue occurs when notifyAll() is called:
java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll()
Could anyone help me to understand how to lock/unlock properly in this example?
Many thanks in advance
public static void setUpdateVar() {
synchronized (isUpdateNeeded){
while(isUpdateNeeded == true){
try {
isUpdateNeeded.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isUpdateNeeded = true;
isUpdateNeeded.notifyAll();
}
}
private class UpdateVar extends Thread {
boolean isRunning = true;
public void run() {
while(isRunning) {
synchronized (isUpdateNeeded){
if (isUpdateNeeded == true){
isUpdateNeeded = false;
mHandler.post(new Runnable() {
public void run() {
ShowList();
}
});
}
}
}
}
}