Java: While loop does not exit -


does 1 have idea why while exits if theres system.out.println()?

the same code doesn't work if comment out println()

do{      system.out.println("");      if(haswon()){         inout.out(output() + "\nyou won");         system.exit(0);     } } while (!haswon())  

the program follows

static final int gridsize = integer.parseint(inout.in(1, "enter grid size")); static tile[][] board = new tile[gridsize][gridsize]; static int wincond = 1; static guiframe f = new guiframe(gridsize); static btnpanel p = new btnpanel(gridsize); static jbutton[][] btn = new jbutton[gridsize][gridsize];  public static void main(string[] args) {      //creating objects     (int = 0; < gridsize; i++) {         (int z = 0; z < gridsize; z++) {             board[i][z] = new tile();         }     }     gui();      while (!haswon()) {          system.out.println("");          if(haswon()){             inout.out(output() + "\nyou won");             system.exit(0);         }     } }  public static boolean haswon() {     boolean haswon = true;      (int = 0; < gridsize; i++) {         (int z = 0; z < gridsize; z++) {             haswon = haswon && (board[i][z].getstatus() == wincond);         }     }      return haswon; }  public static string output() {     string message = "";      (int = 0; < gridsize; i++) {         (int z = 0; z < gridsize; z++) {             message += board[i][z].getstatus() + " ";         }         message += "\n";     }      return message; }  public static void gui() {      (int = 0; < gridsize; i++) {         (int z = 0; z < gridsize; z++) {             string btnvalue = "";             btnvalue += board[i][z].getstatus();             btn[i][z] = new jbutton();             btn[i][z].settext(btnvalue);             btn[i][z].addactionlistener(f);             p.add(btn[i][z]);         }     }      f.add(p, borderlayout.center);  }  public static void modifygui(int i, int z){     btn[i][z].settext(string.valueof(board[i][z].getstatus())); } 

this puzzle game user clicks tile , adjacent tiles change well. when puzzle completed, without println() not show completion, println() exits loop , exits program.

to re-emphasize, works fine if theres println() inside while loop. when comment out doesnt work.

what have tight cpu intensive loop repeatedly calling haswon(). looks other thread responsible updating board state results in "won" position.

it looks problem tight cpu loop causing other thread starve, , inserting println sufficient allow thread scheduler reschedule.

another possible explanation have shared data structure 1 thread reading , updating ... without synchronization. in theory, fact not synchronizing mean changes made updating thread never seen reading thread, because compiler has not seen need insert "barrier" instructions cause relevant cache flushing, etc.


as @chrylis notes, busy looping horribly inefficient. need replace busy looping:

  • a hacky solution put thread.sleep() call loop (instead of println call).

  • a better solution use object.wait() , object.notify(). main thread calls wait() on mutex object after each check, , thread doing updating calls notify() on same mutex each time update might result in "won" position.

the second solution deals synchronization issue. wait() , notify() methods can performed while holding mutex; i.e. in synchronized block or method.


here code snippets show how wait / notify can implemented.

    public static final object mutex = new object();     public static boolean finished = false;      // 1 thread     synchronized (mutex) {         while (!finished) {              mutex.wait();         }     }      // thread     synchronized (mutex) {         finished = true;         mutex.notify();     } 

obviously, use of statics illustrative purposes.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -