Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Is the while loop of NIO suppose to be inside a Thread?:

Reply
Thread Tools

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

 
 
-
Guest
Posts: n/a
 
      07-05-2005
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?
 
Reply With Quote
 
 
 
 
Esmond Pitt
Guest
Posts: n/a
 
      07-05-2005
- 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
NIO with timeouts != NIO? iksrazal Java 1 06-18-2004 02:28 PM



Advertisments