java - Code not working unless a simple print statement is inserted in while loop -


this question has answer here:

i have following piece of code

public string ls() throws ioexception, interruptedexception {     string pwd = getpwd();     string inodepath = "upload/files" + pwd + "inode";      // request inode file first     peer.setfilereceivedcheck(false);     inode = new inode();     i.requestinode(pwd + "inode");      boolean filecheck = peer.getfilereceivedcheck();     system.out.println(filecheck);     while (filecheck == false) {         system.out.println(); // not work if line removed!!         filecheck = peer.getfilereceivedcheck();     }     system.out.println(filecheck);      return i.readfrominode(inodepath); } 

in above java method, filecheck variable keeps track of when file downloaded network , becomes true when file download completes; functions returns when file download completed.

the weird problem facing if remove above line (the 1 println); method stuck in while loop , not return though file has been downloaded! idea why happening?

edit: not running in separate thread. file download code in class (peer.java) running in separate. again, getter filecheck out of thread.

please, in name of $deity, not use hot polling. stop. right now. wasting billions of cycles, make whole system less responsive, while wait come on network. use callback - in 2014, it's not rocket science.

that said, repace contents of while loop this:

object o = new object(); synchronized(o) {    filecheck = peer.getfilereceivedcheck(); } 

what happens check returns non-volatile field, , compiler free optimize away - unless code hits synchronization point.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -