• Post Reply Bookmark Topic Watch Topic
  • New Topic

[ nio ] Why should I use non-blocking ServerSocketChannels ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to understand the need for using non-blocking ServerSocketChannels.

As far as I could understand, prior to NIO, if a server needs to managed multiple incoming connections, it had to dedicate a thread per connection. This is due to the fact that it blocks on the accept.

With the introduction of non-blocking sockets, we can circumvent this problem. One thread can manage several socket connections.

My problem domain: I want to write a server app that listens on just one port. It services any connection request and delegate the processing to an individual Worker Thread. Do I need to use non-blocking and selection readiness for this domain ? What advantage will I reap from such a design ?

Thanks

Pho
 
Cal Watson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it create a worker thread per connection? If it does I doubt that will scale well with a 1000+ connections. I have been working on a groupware server using the nio packages, and it has really decreased code complexity and threading issues. I definitly favor it over the old io.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am using the worker-per-connection design. Are you using an event-dispatch design for your server ?

Pho
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cal, could you share some samples of how you used NIO? Here's how I used normal sockets & threads:

I used to create a new thread per request. After moving to JDK 5 I tried a thread pool. Took about a minute to implement!
[ November 04, 2004: Message edited by: Stan James ]
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cal, I'm also interested in the threading issues you saw with the old io package.

Stan, I think Cal is thinking of applications that need to support thousands of simultaneous connections?
 
Cal Watson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren,
My main issue with threading in the old packages was managing over thousands of threads at a time. I felt that there was not a great way to do this with the old io packages. Although my application is still in the early phase, the server only uses one thread and is as efficent as my old implementation.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've looked at a couple of high performing nio frameworks, most of them use an event driven model. Read this onjava article. It seems quite complicated. But it's probably the way to go to support several thousands of simultaneous connections.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys. Yeah, I have a user community of under a hundred, concurrent use is very low. I liked the thread topic - show me why I want to care about NIO - and I think you did that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!