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

Tuesday, May 08, 2007

Programming 2D Games

Are you interested in programming 2d games? Got a PC, Mac, or linux box? That all you need with the new SwinGame API.

The SwinGame API was developed initially by Daniel Chambers as a Windows API for developing small games. Over the last few weeks I have been modifying the code to extend its features, and to make it cross platform. The latest version is still in testing, but is worth looking at if you are interested in creating a 2D game.

SwinGame features:

  • Window drawing is double buffered
  • Create a Window of any size
  • Load and display images of various formats including bmp, png, and jpg
  • Works with 32 bit images, including alpha blending
  • Load true type fonts
  • Read and write text
  • Draw lines, and shapes
  • Sprite management routines
  • Load and play sounds in various formats including wav, ogg, and mp3
  • Detect collisions between sprites, bitmaps, and areas of the screen
  • Perform pixel level collision detection
  • Vector manipulation routines
  • Use matrices to manipulate game vectors
There are a couple of games in the process of being built by the 1st year PSD students at the moment, and I will make these available for download from SwinBrain as soon as they are complete.