Thursday, May 10, 2007

To Thread or Not To Thread

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...

6 comments:

Andrew Cain said...

If you use MINA (look in its Documentation-Quick Start) I recommend that you look at using ANT. With MINA you need to link to a number of JAR files, ant will do this for you! Use ANT! Don't know how... read this wiki post

Anonymous said...

Nice post, Andrew. I know for sure heaps of data comms groups are struggling a lot with the filesharing assignment. Hopefully this will give them a good helping hand

Anonymous said...

Sounds like data comms guys have some really cool projects. Shame I'm not doing it :(

Azureus is an open source bittorrent client written in java perhaps they could get some idea's from there :)

http://azureus.sourceforge.net/

Andrew Cain said...

Thats a great idea Brent... didn't think of that.

Joost said...

Thank you for this post Andrew. Really clears things out. I think a lot of us are pretty freaked out about the deadline. So any help is great, even if they are suggestions as to where to look.

Anonymous said...

I should've read this blog before doing the DCS assignment. Would've been useful :p We settled for threads in the end though... I'll make sure i use Non-Blocking IOs next time.