Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Giovanni Azua Newsgroups: comp.lang.java.programmer Subject: Blocking IO thread-per-connection model: possible to avoid polling? Date: Tue, 27 Sep 2011 00:09:07 +0200 Lines: 55 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Trace: individual.net LdRKt8kPu/zmzLgMYU1KyQT4uNuzCn3ySnqdQ2ir3TqRCMdIxC Cancel-Lock: sha1:3+zXmh7ASWyfNPJGc8iBt6zvjQg= User-Agent: Microsoft-Entourage/12.31.0.110725 Thread-Topic: Blocking IO thread-per-connection model: possible to avoid polling? Thread-Index: Acx8mOjUpI54Z7VRy0m+w52qv89HCA== Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8344 Hello, I'm firstly implementing this "thread-per-connection model" on the Server component where one thread is responsible for reading the requests and sending results back to the client, it is not responsible for actually processing the requests though. I can gracefully (cleanup) stop the thread by doing socket.getChannel().close() see the snippet below. However, in order to send data, I also need to interrupt the Thread while it is blocked waiting for input. Apparently the only way to do this without closing the channel as side effect is to do polling? TIA, Best regards, Giovanni ObjectInputStream in = null; try { in = new ObjectInputStream(clientSocket.getInputStream()); while (true) { try { // >>> it is blocked here <<< MessageData data = (MessageData) in.readObject(); // add requests to the BlockingQueue for processing requestQueue.add(new Request(data, this)); // >>> send stuff here <<< // if (resultAvailable()) { // out.writeObject(result); // } } catch (ClosedChannelException exception) { // stop requested break; } } } catch (IOException exception) { exception.printStackTrace(); throw new RuntimeException(exception); } catch (ClassNotFoundException exception) { exception.printStackTrace(); throw new RuntimeException(exception); } finally { try { in.close(); } catch (IOException exception) { exception.printStackTrace(); } }