Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Programming Java threads in the real world, Part 8

Threads in an object-oriented world, thread pools, implementing socket 'accept' loops

  • Print
  • Feedback
One of the biggest problems associated with thread use in an object-oriented environment is a conceptual one: though procedural programmers naturally think about the flow of control from function to function as the system works, object-oriented designers focus on the message flow within an individual scenario or use case. The traditional view of threading, however, concerns itself entirely with flow of control. As a consequence, object-oriented designers typically don't think about threads -- at least not until they get down to the very low-level implementation part of the design; rather, they think about two categories of messages: synchronous messages that don't return until they're done doing whatever they do, and asynchronous messages, which initiate some background operation and return immediately. This month's column (along with next month's) will address such issues by showing you how to reconcile these two points of view and implement object-oriented-style threading using Java's essentially procedural implementation of threads.

Java's implementation of threading, in fact, complicates matters by using a misleading metaphor for threads. You have to create a thread by deriving from Thread, which leads many a novice Java programmer to the erroneous belief that all the methods of the Thread derivative will run on that thread. In fact, a method of a Thread derivative is just like any other method: it runs on a thread only if called directly or indirectly from that thread's run() method. Objects do not run on threads; methods do.

It's sometimes difficult to predict any control flow in an object-oriented system. In a procedural system, one procedure just calls another, beginning at a single starting point. If there are many threads, there are many paths through the code, but each path starts at a known place, and the control flow through a given thread is predictable (though sometimes not easily so). Object-oriented systems are another matter. Object-oriented systems tend to be networks of cooperating objects, communicating with one another via some message-passing system. The system's "main" method may well do nothing but create a bunch of objects, hook them up to each other, and then terminate. Flow of control is very difficult to predict in this system.

Synchronous vs. asynchronous messages

As I mentioned earlier, an object-oriented designer looks at the world in terms of objects and messages. Objects pass messages to each other, and the receipt of some message causes an appropriate message-handler -- a Java method -- to be executed. Most of these messages are synchronous: their handlers don't return until they're finished doing what they do. Other messages are asynchronous: the handler returns immediately, before the requested operation completes. Meanwhile, work is going on in the background to satisfy the original request. A good example of an asynchronous message in Java is Toolkit.getImage(), which initiates the process of fetching an image and then returns immediately, long before the actual image arrives.

  • Print
  • Feedback

Resources
  • Links to all previous articles in this series can be found in the "Articles" section of Allen's Web site. You can find downloadable version of all the code in the same place. http://www.holub.com
  • "Sockets programming in JavaA tutorial," Qusay H. Mahmoud (JavaWorld, December 1996) Get up to speed on Java's socket support with this tutorial. http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
  • "Write your own threaded discussion forumThe communications and server components, Part 2," Michael Shoffner (JavaWorld, March 1997). http://www.javaworld.com/javaworld/jw-03-1997/jw-03-step.html
  • Java Network Programming, Elliotte Rusty Harold (O'Reilly, 1997). http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=1565922271
  • Design Patterns Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison Wesley, 1994). The Command design pattern is presented. This book is essential reading for any object-oriented designer. http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201633612