java - Read from stdin but unable to know when to stop -


i running commnads on commmand prompt. waiting last command's output complete. have read output , perform operation. command's output dynamic , can not predict when can stop reading.

i having issues dont know when stop reading. if suppose keep while read(), last command output not ending new line. there mechenism can tell me if there has been no activity on stdin last 5mins, alert??

the approach took create class implementing runnable monitors value of shared atomicinteger flag. inputrunnable class sleeps 5 minutes (300000 ms) , wakes check whether value has been set main method. if user has entered @ least 1 input in last 5 minutes, flag set 1, , inputrunnable continue execution. if user has not entered input in last 5 minutes, thread call system.exit() terminate entire application.

public class inputrunnable implements runnable {     private atomicinteger count;      public inputrunnable(atomicinteger count) {         this.count = count;     }      public void run() {         {             try {                 thread.sleep(300000);               // sleep 5 minutes             } catch (interruptedexception e) {                 // log error             }              if (count.decrementandget() < 0) {      // check if user input occurred                 system.exit(0);                     // if not kill application             }         } while(true);     } }  public class mainthreadclass {     public static void main(string args[]) {         atomicinteger count = new atomicinteger(0);         inputrunnable inputrunnable = new inputrunnable(count);         thread t = new thread(inputrunnable);         t.start();          while (true) {             system.out.println("enter number:");             scanner in = new scanner(system.in);             int num = in.nextint();                 // scan user input              count.set(1);         }     } } 

i tested code locally , appears working, please let me know if have issues getting run on system.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -