O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12. Implementing Threading

In Chapter 11, we discussed the support Java provides for threading, both in the language and in the core libraries. In this chapter, we take a much more pragmatic point of view. The heart of this chapter is a set of idioms and usage guidelines intended to help you use threads safely and effectively in your applications. As part of this, we’ll reimplement the bank example as a threadsafe application and, as a final example, talk about how to implement a general-purpose pooling mechanism. By the end of this chapter, you should have a complete understanding of how to use threads in a distributed application.

The Basic Task

Let’s walk through the bank example again, this time looking at everything from the point of view of threads. The first problem is, of course, to discover where the threads are. Recall that we have three basic executing pieces of code: the server, the client, and the launch code.

Of these, the server must be absolutely threadsafe. As we discussed in Chapter 11, RMI creates multiple threads. It will gleefully dispatch multiple threads into a single server object if multiple clients simultaneously make method calls on it. If you don’t take this into account while implementing the server objects, the application will fail.

The launch code, on the other hand, rarely needs to use multiple threads. It’s executed once to configure and launch the application. Launch code is not complex, nor does it need to handle multiple tasks simultaneously. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required