• Post Reply Bookmark Topic Watch Topic
  • New Topic

NIO Selector - single threaded use  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java class, Foo, will create a non-blocking SocketChannel to a network service. I save the SocketChannel to a member variable. Foo can perform two operations: opA() and opB(). Inside opA() and opB() I will do something like this:

The question I have is this: if opA and opB are using the same SocketChannel, do I need to do any synchronization on the methods ? The reason I'm asking is because I've heard that Async I/O will allow me to dispense with creating multiple threads and multiple connections by multiplexing everything on a single connection.
Please enlighten me.
Thanks
Pho
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nio package won't dispense you from creating multiple connections, but from creating multiple threads. You could even have a single thread ready for reading/writing to multiple connections, and waiting for the the pending connections to a ServerSocketChannel to be accepted; all of these as indicated by a Selector.
Please read the NIO examples provided in the SDK Documentation.
The book The JDK1.4 Tutorial by Gregory M. Travis has also a chapter about NIO in which a server example appears.
[ March 30, 2004: Message edited by: Jose Botella ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!