I have a class called communicator. This class is a listener of a thread receiving events from another program. Also this class has a method call refresh that sends and action to the program a waits for the response that comes through the listener.
Both methods are in the same class but called by diferent threads.
public void processRefreshEvent(ManagerEvent event){ //processing event //... //I'm done notify(); } public synchronized void refresh() throws Exception { isRefreshing = true; try { manager.send(new refresh()); } catch (ManagerException e) { isRefreshing = false; } try { wait(5000); } catch (InterruptedException e) { } finally{ isRefreshing = false; } }
when executing the code above I get the follow exception:
java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at Communicator.refresh(Communicator.java:203) ...
What is the proper way to ‘wait’ for another thread to finish. Thanks.
You need to synchronize your threads on a monitor. For instance (using the current object as the monitor):