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

Threading Concepts

Writing a multi-threaded program is hard.

Writing a correct multi-threaded program is impossible.

—seen in an email signature

So far, this all seems reasonable. A program is attempting to do several things at once. It’s connected to a set of clients and devices. Since the clients and devices are acting independently of each other, it makes sense that the program’s runtime structure reflects this. Threads are simply a nice abstraction that helps keep the clutter down. Why, then, do threads have such a bad reputation? Why do most people frequently make mistakes when they try to write multithreaded code?[64]

Tip

The vocabulary associated with threading can be confusing. In this book, I use the term multithreaded code to refer to code that necessarily has several threads running through it and still executes correctly. I call code that can have multiple threads running through it, but doesn’t necessarily involve more than one thread, threadsafe. Furthermore, I will frequently say that a thread is in an object or method, or active in an object or method. This simply means that the top frame of the calling stack involves that particular instance and method.

The problem with threads is that they can interfere with each other. Consider our bank account example again. Account_Impl contains the following code:

public class Account_Impl extends UnicastRemoteObject implements Account { private Money _balance; public Account_Impl(Money startingBalance) throws RemoteException ...

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