java - How to bypass scanner check next element (if that makes sense) -


i trying create continuous thread server recieves/sends messages client when try check next element gets stuck:

public void run() {     try     {         try         {             arraylist<socket> connections = parent.getconnections();             in = new scanner(socket.getinputstream());              while(true)             {                 if(in.hasnextline()) // gets stuck here                 {                     string message = in.nextline();                     system.out.println("client said " + message);                 }             }         }                  {             socket.close();         }     }      catch(exception e)     {         e.printstacktrace();     } 

how make loop not stuck @ specified point

assuming want able deal 'lines', i'd start this:

public class socketreader implements runnable {      private final inputstream stream;     private final queue<string> destination;     private volatile boolean active = true;      private socketreader(inputstream stream, queue<string> destination) {         this.stream = stream;         this.destination = destination;     }      public static socketreader getreader(socket toread, queue<string> destination) throws ioexception {         return new socketreader(toread.getinputstream(), destination);     }      public void shutdown() {         active = false;     }      public void run() {         while(active) {             if (stream.hasnextline() && active) {                 final string line = stream.nextline;                 destination.add(line);             }         }         try {             stream.close();         } catch (ioexception e) {             // log somewhere         }     } } 

drop own thread (or part of thread or executor pool, really), , you've made rest of application non-blocking regards code. expect block while waiting updates stream.hasnextline(). can supply blockingqueue if don't wish actively poll queue, handling updates in other fashion.

you can output:

public class queuedprinter implements runnable {      private final queue<string> input;     private final printstream destination;     private volatile boolean active;      public queuedprinter(queue<string> input, printstream destination) {         this.input = input;         this.destination = destination;     }      public void shutdown() {         active = false;     }      public void run() {         while(active) {             final string line = input.poll();             if (line != null && active) {                 destination.println(line);             }         }     }  } 

please note haven't tested this, , may have adjust things other checked exceptions. need put in additional error-checking code (null-handling comes mind). also, isn't completely threadsafe, 'good enough' uses.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -