java - IllegalMonitorStateException when attempting to wait() a thread -


this question has answer here:

i have done research problem , still unable fix issue @ hand. threading new me im having trouble comprehending. in program, starting thread transferring files on timed period. swt being used gui in program. in main ui code have pause , play button. play button , related code:

    playbutton.addselectionlistener(new selectionadapter() {         @override         public void widgetselected(selectionevent e) {              if(isrunning){                 // todo implies runningthread waiting, notify             }else{                 playbutton.setenabled(false);                 pausebutton.setenabled(true);                 try {                     play();                 } catch (ioexception e1) {                     e1.printstacktrace();                 }             }         }     });  public void play() throws ioexception{      if(controller.timman.geteventsendpreferences().equalsignorecase("manual")){         isrunning = true;         manualthread.start();       }else if(controller.timman.geteventsendpreferences().equalsignorecase("timed")){         isrunning = true;         timerthread.start();     }      return; } 

timerthread implemented such:

timerthread = new thread(new runontimer(controller));  public static class runontimer implements runnable{      scriptcontroller controller;      public runontimer(scriptcontroller c){         controller = c;     };      @override     public void run(){         try{             synchronized(this){                 controller.runontimer();             }         } catch (ioexception e) {             e.printstacktrace();         }     } }; 

and here runontimer() function called in run:

public void runontimer() throws ioexception{      for(file f : dirman.geteventfilelist()){         int randomtimevalue = 0;             int range = timman.getuppertimerbound() - timman.getlowertimerbound();             if(range > 0)                 randomtimevalue = (new random().nextint(timman.getuppertimerbound() - timman.getlowertimerbound()) + 0);              try {                 thread.sleep(1000 * (randomtimevalue + timman.getlowertimerbound()));             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              dirman.updatefilecounts();             system.out.println("event " + dirman.getsenteventfiles());             file dest = new file(dirman.getdestinationfolder() + "\\" + f.getname());             files.copy(f.topath(), dest.topath(), standardcopyoption.replace_existing);     }        updatefilecounts();     return; } 

the problem starts not while thread running when call thread wait in paused buttons listener implementation.

pausebutton.addselectionlistener(new selectionadapter() {          @override         public void widgetselected(selectionevent e) {             if(timerthread.isalive()){                 try {                     timerthread.wait();                 } catch (interruptedexception e1) {                     // todo auto-generated catch block                     e1.printstacktrace();                 }             }              pausebutton.setenabled(false);             playbutton.setenabled(true);         }      }); 

in other questions have read , google has results have showed, synchronizing problem when comes error. getting owner of objects monitor. again have not worked threading concept of object monitors mystery me. after reading in on these tried using synchronized in run() method of runontimer class didn't seem change when attempting 'pause' (ie. make thread wait).

what missing or doing wrong. program work fine , thread run expected except error of course.

if read documentation object.wait see crucial piece of information:

the current thread must own object's monitor.

what means have have synchronized block on object call wait. documentation, actually, has nice pseudo code shows correct usage of wait

in case:

synchronized(timerthread) {   while( shouldstillwait() )   {     timerthread.wait();   } }  boolean shouldstillwait( ) {    ... } 

also there should place somewhere in code calls timerthread.notify(), without wait forever.

that said, since java 5, there better built-in synchronization primitives make use of wait/notify pretty thing of past.

and thing, because wait/notify brittle , error prone.

i suggest reading excellent 'java concurrency in practice' brian goetz, familiar new synchronization primitives , style multi-threaded programming.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -