Is the while loop of NIO suppose to be inside a Thread?:

Discussion in 'Java' started by -, Jul 5, 2005.

  1. -

    - Guest

    Is the while of NIO suppose to be inside a Thread?

    public SomeClass() {

    ...

    thread = new Thread(new Runnable() {
    while (selector.select() > 0) {
    ...

    if (key.isWritable()) {
    socketChannel.write(byteBuffer);
    }

    ...
    }
    }
    thread.start();
    }

    public void sendData(String string) {
    byteBuffer = ByteBuffer.wrap(string.getBytes());
    }


    If I don't use a thread, calling sendData(..) is to no avail. Am I
    missing something?
    -, Jul 5, 2005
    #1
    1. Advertising

  2. -

    Esmond Pitt Guest

    - wrote:
    > Is the while of NIO suppose to be inside a Thread?
    >
    > public SomeClass() {
    >
    > ...
    >
    > thread = new Thread(new Runnable() {
    > while (selector.select() > 0) {
    > ...
    >
    > if (key.isWritable()) {
    > socketChannel.write(byteBuffer);
    > }
    >
    > ...
    > }
    > }
    > thread.start();
    > }
    >
    > public void sendData(String string) {
    > byteBuffer = ByteBuffer.wrap(string.getBytes());
    > }
    >
    >
    > If I don't use a thread, calling sendData(..) is to no avail. Am I
    > missing something?


    As socket channels are almost always writable there isn't much point in
    any of this. Do the wrap, try the write: if it doesn't complete,
    register for OP_WRITE and wait for it to fire; when it does, continue
    the write, if it completes now deregister OP_WRITE otherwise repeat the
    select(). The select() part will only really happen if you overrun the
    sender. Whether you do that in the same thread or another thread is up
    to you.

    EJP
    Esmond Pitt, Jul 5, 2005
    #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. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,218
    iksrazal
    Jun 18, 2004
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,215
    Tim Slattery
    Mar 30, 2005
  3. Uday Bidkar
    Replies:
    4
    Views:
    474
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. John  Hartnup
    Replies:
    10
    Views:
    624
    Esmond Pitt
    Apr 25, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page