Problem reading from nio socketchannels into a bytebuffer

Discussion in 'Java' started by Jeff Silvis, Sep 16, 2004.

  1. Jeff Silvis

    Jeff Silvis Guest

    At:
    http://www.javanio.info/filearea/bo...ronsoft/books/nio/channels/SelectSockets.java

    There is sample code. In that page there are the lines:




    // send the data, don't assume it goes all at once
    while (buffer.hasRemaining()) {
    socketChannel.write (buffer);
    }
    // WARNING: the above loop is evil. Because
    // it's writing back to the same non-blocking
    // channel it read the data from, this code can
    // potentially spin in a busy loop. In real life
    // you'd do something more useful than this.




    In some of my code I have written a line more or less exactly like:
    while (buffer.hasRemaining()) {
    socketChannel.write (buffer);
    }


    and the code does just spin. What is an example of "something more
    useful than this"?

    Jeff
    Jeff Silvis, Sep 16, 2004
    #1
    1. Advertising

  2. Jeff Silvis

    Paul Lutus Guest

    Jeff Silvis wrote:

    / ...

    [ about non-blocking I/O methods ]

    > In some of my code I have written a line more or less exactly like:
    > while (buffer.hasRemaining()) {
    > socketChannel.write (buffer);
    > }
    >
    >
    > and the code does just spin. What is an example of "something more
    > useful than this"?


    Obviously if you plan to use this kind of code, put it in a separate thread
    and use the blocking version of these I/O classes. What you in essence have
    done is use a non-blocking method and have arranged to block with it, in a
    particularly wasteful way.

    To put it another way, you never want to loop on a non-blocking method,
    because the method won't block for you. Your code is written to accommodate
    a conventional blocking I/O call.

    So please answer this question. Did you really want a blocking method and
    chose this one by mistake, or did you really want to learn how to use
    non-blocking methods, and chose this loop structure by mistake (even if
    from an "official " Web site)?

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Sep 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael B Allen

    [nio] Mutliple Messages in ByteBuffer

    Michael B Allen, Dec 13, 2004, in forum: Java
    Replies:
    1
    Views:
    414
    Esmond Pitt
    Dec 13, 2004
  2. Remon van Vliet

    nio ByteBuffer filling

    Remon van Vliet, May 19, 2005, in forum: Java
    Replies:
    3
    Views:
    2,226
    John C. Bollinger
    May 19, 2005
  3. Replies:
    2
    Views:
    1,461
  4. Replies:
    5
    Views:
    4,169
  5. Andersen

    SocketChannels NIO

    Andersen, Jan 21, 2006, in forum: Java
    Replies:
    3
    Views:
    346
    Thomas Hawtin
    Jan 23, 2006
Loading...

Share This Page