• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NIO Selector - single threaded use

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic