jQuery in Action, 2nd edition
The moose likes Sockets and Internet Protocols and the fly likes NIO Selector - single threaded use Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "NIO Selector - single threaded use" Watch "NIO Selector - single threaded use" New topic
Author

NIO Selector - single threaded use

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

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


Regards,

Pho
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: NIO Selector - single threaded use
 
Similar Threads
ServerSocketChannel.accept() seems to create a random port to send data to?
Oh, God I NEED Help!!!!!!!!
to and fro out of a loop to a method in back?
OOP Is Much Better in Theory Than in Practice
Problems in inserting data to MS Access Database