Java making the code wait until Swing-Timer finished work -


i have method start several amount of timers. work 1 after (i increase time wait each timer).

i need programm wait untill last timer finished work! otherwise programm recursivly called infinitely.

here method:

public void dogamelogic(int cellid) {     final controllerhelper helper = new controllerhelper();     helper.setindex(0);     helper.setcellid(cellid);     helper.setdecrement(false);     helper.setfinished(true);      if(actualgamestate.getplayersturn() == modelconstants.player_one) {         helper.setactualcoinlist(actualgamestate.getcoinsplayerone());     } else {         helper.setactualcoinlist(actualgamestate.getcoinsplayertwo());     }      if(!(helper.getactualcoinlist().get(helper.getcellid()) == 0)) {         if(cellid > ((helper.getactualcoinlist().size() / 2) - 1)) {             helper.setindex(cellid + 1);         } else if(cellid < (helper.getactualcoinlist().size() / 2)) {             helper.setindex(cellid - 1);         }          if(helper.getindex() < (helper.getactualcoinlist().size() / 2)) {             helper.setdecrement(true);         } else if(helper.getindex() > ((helper.getactualcoinlist().size() / 2) - 1)) {             helper.setdecrement(false);         }          int coins = (helper.getactualcoinlist().get(cellid));         int time = 500;          for(int = 0; < coins; i++) {             timer timer = new timer(time, new actionlistener() {                 public void actionperformed(actionevent e) {                     int tempdec = (helper.getactualcoinlist().get(helper.getcellid()) - 1);                     helper.getactualcoinlist().remove(helper.getcellid());                     helper.getactualcoinlist().add(helper.getcellid(), tempdec);                      if(helper.getindex() > (helper.getactualcoinlist().size() - 1)) {                         helper.setindex(((helper.getactualcoinlist().size() / 2) - 1));                          helper.setdecrement(true);                     } else if(helper.getindex() < 0) {                         helper.setindex((helper.getactualcoinlist().size() / 2));                         helper.setdecrement(false);                     }                      if(!helper.isdecrement()) {                         int tempinc = (helper.getactualcoinlist().get(helper.getindex()) + 1);                          helper.getactualcoinlist().remove(helper.getindex());                         helper.getactualcoinlist().add(helper.getindex(), tempinc);                          helper.setindex(helper.getindex() + 1);                     } else {                         int tempinc = (helper.getactualcoinlist().get(helper.getindex()) + 1);                          helper.getactualcoinlist().remove(helper.getindex());                         helper.getactualcoinlist().add(helper.getindex(), tempinc);                          helper.setindex(helper.getindex() - 1);                     }                      setactualgamestate(actualgamestate);                 }             });              timer.setrepeats(false);             timer.start();              time += 1000;         }          if(!helper.isdecrement()) {             helper.setindex(helper.getindex() - 1);         } else {             helper.setindex(helper.getindex() + 1);         }          if(!(helper.getactualcoinlist().get(helper.getindex()) < 2)) {             dogamelogic(helper.getindex());              helper.setfinished(false);         }          if(helper.isfinished()) {             if(this.actualgamestate.getplayersturn() == modelconstants.player_one) {                 this.actualgamestate.setplayersturn(modelconstants.player_two);              } else {                 this.actualgamestate.setplayersturn(modelconstants.player_one);             }              this.gameinfos.getgamehistory().putstate(this.actualgamestate);             this.setactualgamestate(this.actualgamestate);         }     } else {      } } 

obviously cannot make thread wait long timer.isrunning() true because freez gui or kill programm.

isnt there way make program wait timers?

you use countdownlatch.

countdownlatch lock = new countdownlatch(coins);  for(int = 0; < coins; i++) {     timer timer = new timer(time, new actionlistener() {         public void actionperformed(actionevent e) {          /* actionlistener code here */          lock.countdown(); //signal countdownlatch timer finished         }     });      timer.setrepeats(false);     timer.start();      time += 1000; }  try {     lock.await(); //waits until countdownlatch has been counted down "coins" number of times } catch (interuptedexception ex) {     ex.printstacktrace(); //or more sophisticated handling of error } 

however, executing dogamelogic() on edt? if described above on edt, gui hang several seconds. should never wait extensive amounts of time on edt. tasks performed on edt should small , quick, or performance of gui disastrous.

if case, should redesign threading strategy of application tasks require extensive amounts of time due waiting complete performed on non-edt threads, , merely submit small task update gui when time consuming part finished.

<edit>

check this article brief description of problem. under headling "never delay edt"

basically, when comes time consuming tasks, common way use swingworker perform time consuming computation / waiting / file operation / download etc.

the time consuming non-gui-related task should performed in doinbackground() method, invoked on non-edt thread. subsequent necessary gui updates can performed in done() method invoked on edt when background work has finished. full example, in small article linked above.

</edit>


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -