I am trying to synchronize different threads in my application. Basically there are some threads that should be able to block the application from closing. Here is what I have:
public class Test{
public static void main(String args[]){
Syncer syncer = new Syncer();
Object o = new Object();
syncer.lockExit(o);
//.. do some stuff//
syncer.unlockExit(o);
}
}
public class Syncer {
private List<Object> l_Exit;
public Syncer() {
l_Exit = new ArrayList<Object>();
}
public synchronized void lockExit(Object o) {
l_Exit.add(o);
}
public synchronized void unlockExit(Object o) {
l_Exit.remove(o);
if (l_Exit.isEmpty()) {
l_Exit.notifyAll();
}
}
public synchronized void waitForExit() {
while (!l_Exit.isEmpty()) {
try {
l_Exit.wait();
} catch (InterruptedException ex) {
Test.log.log(Level.SEVERE, null, ex);
}
}
}
}
I get IllegalMonitorException when running syncer.unlockExit(o);