Threads are simple. They allow a program running on a single computer to perform more than one task at a time in much the same way that an operating system allows more than one program to run at the same time. Using threads, however, can involve some subtlety. The Java programming language has extensive support for threading, both in the programming language and in the libraries that come with the JDK. In this chapter, we’ll discuss why threading is important in distributed programming and cover the fundamentals of using threads in Java.
In our previous discussions on deciding on a server, I talked briefly about several issues related to scalability. The goal of those discussions was simple: we wanted to guarantee that our systems will be able to simultaneously support many different users, all of whom are attempting to perform different tasks.
But those discussions carefully dodged a very difficult question: how do we build a server that can handle more than one request at once? Consider the bank system we built in Chapter 9. If we take the existence of multiple clients into account, we wind up with the architecture diagram shown in Figure 11-1.
Figure 11-1. Architecture diagram for the bank system
This has an important implication: the
possibility exists that two separate clients will
receive stubs for the same instance of
Account (e.g., ...