java - IllegalMonitorStateException in code -


class test {      public static void main(string[] args) {          system.out.println("1.. ");         synchronized (args) {              system.out.println("2..");              try {                 thread.currentthread().wait();             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              system.out.println("3..");         }      } } 

i getting illegalmonitorstateexception monitor exception in code. per understanding, because of synchronized block around args string array object, current thread must have acquired lock , wait method, release lock.

can explain me reason behind exception?

you're calling wait() on thread.currentthread(). before calling wait() on object, must own monitor of object, way of synchronized block synchronizing on object. missing

synchronized(thread.currentthread()) {     thread.currentthread().wait(); } 

that said, calling wait() on thread object not should do, , shows have not understood wait() does, given don't have other thread calling notify() or notifyall(). synchronizing on arguments passed main method strange choice. wait() low-level method should used, if understand does. better answer, should explain want code do.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -