Today I have been asked a few threading questions from students studying at Swinburne. Usually these were about the "How to" do something with threads, when I think the real question was should threads be used at all?
From what I know these students are developing a torrent like application and network protocol. The issue is that they need to accept data from a number of peers at the same time. Initially this appears to suggest the need for threads... but are they really needed?
In this case I think not. The project indicate potentially thousands of peers... a threaded client with one thread per client will kill the machine with context switches before any real work can be done. So what is the answer? Non-Blocking IO.
With Non-Blocking IO you dont need to block, waiting for data to arrive from a peer. Rather you keep a list of connected peers and then loop through those that have data currently available. This can all be performed on a single thread, giving good performance and throughput.
Java offers a number a library of non-blocking IO utilities. Have a look at the NBTimeServer.java example. This shows a time server that accepts connections using non-blocking IO. A quick look through the Java API and you will find the SelectableChannel and the SelectionKey class. Using the register methods, and the various SelectionKey options you can loop through only those connections that actually have data waiting to be processed.
There is a good article, with a good source code example at http://tim.owlmountain.com/tutorials/NonBlockingIo.htm though there is an interesting note at the start.
As suggested in the article, you could also look at MINA the Multipurpose Infrastructure for Network Applications. If you use MINA just stick with the basic "getting started" code and build on top of that. There is no real need to worry about fiddling with the thread model. But... check with the subject convener first...
Showing posts with label concurrency. Show all posts
Showing posts with label concurrency. Show all posts
Thursday, May 10, 2007
To Thread or Not To Thread
Posted by
Andrew Cain
at
5:31 pm
6
comments
Labels: concurrency, programming, subjects
Subscribe to:
Posts (Atom)