multithreading - Java shared condition between classes throws IllegalMonitorStateException: null -
i have structure this:
lock wrapper - used store lock, condition , object response
public class lockwrapper{ private lock lock; private condition mycondition; private myobject myobject; public lockwrapper(lock lock, condition mycondition) { this.lock = lock; this.mycondition = mycondition; } public condition getmycondition() { return mycondition; } public myobject getmyobject() { return myobject; } public void setobject(myobject myobject) { this.myobject = myobject; } public lock getlock() { return lock; } }
task - pushed thread pool execution. initiates requests server , waits server responses.
public class mytask implements runnable{ private lock lock = new reentrantlock(); private condition mycondition = lock.newcondition(); private mywebsocketapi api; public mytask(mywebsocketapi api) { this.api = api; } @override public void run() { lock.lock(); try { // long randomlong = generaterandomlong(); api.sendrequest(randomlong, new lockwrapper(lock, mycondition)); mycondition.await(); //do after got response } finally{ lock.unlock(); } } }
websocket - gets requests , notifies tasks responses
public abstract class mywebsocketapi extends websocketclient { //... private map<long, lockwrapper> lockwrappers = new concurrenthashmap<>(); public void sendrequest(long id, lockwrapper lockwrapper){ this.lockwrappers.put(id, lockwrapper); //processrequest } @override public void onmessage(string message) { lockwrapper lockwrapper = lockwrappers.get(message.get(0).getaslong()); lockwrapper.getlock().lock(); try{ lockwrapper.setmyobject(new myobject(message)); this.lockwrappers.put(message.get(0).getaslong(), lockwrapper); lockwrapper.getmycondition().signalall(); } { lockwrapper.getlock().unlock(); } } //... }
line lockwrapper.getmycondition().signalall();
throws exception:
java.lang.illegalmonitorstateexception: null @ java.util.concurrent.locks.abstractqueuedsynchronizer$conditionobject.signalall(abstractqueuedsynchronizer.java:1954)
why conditions throw exception when try notify tasks got objects? did make mistake somewhere or java doesn't allow shared conditions?
it error in task
. problem was creating lock , condition both global , local in method run. lock , condition had same name. in cases using lock
, in cases this.lock
(but 2 different locks). result, in method onmessage
had condition , lock not connected together. after removed duplicates works.
Comments
Post a Comment