Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8344

Blocking IO thread-per-connection model: possible to avoid polling?

From Giovanni Azua <bravegag@hotmail.com>
Newsgroups comp.lang.java.programmer
Subject Blocking IO thread-per-connection model: possible to avoid polling?
Date 2011-09-27 00:09 +0200
Message-ID <CAA6C4A3.7E8F%bravegag@hotmail.com> (permalink)

Show all headers | View raw


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();
            }
        }
 

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

Blocking IO thread-per-connection model: possible to avoid polling? Giovanni Azua <bravegag@hotmail.com> - 2011-09-27 00:09 +0200
  Re: Blocking IO thread-per-connection model: possible to avoid polling? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-26 15:22 -0700
    Re: Blocking IO thread-per-connection model: possible to avoid polling? Robert Klemme <shortcutter@googlemail.com> - 2011-09-27 07:49 +0200
    Re: Blocking IO thread-per-connection model: possible to avoid polling? Giovanni Azua <bravegag@hotmail.com> - 2011-09-27 07:52 +0200
      Re: Blocking IO thread-per-connection model: possible to avoid polling? Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-27 09:29 -0700
        Re: Blocking IO thread-per-connection model: possible to avoid polling? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-09-27 10:23 -0700
        Re: Blocking IO thread-per-connection model: possible to avoid polling? Giovanni Azua <bravegag@hotmail.com> - 2011-09-29 22:21 +0200

csiph-web