Chapter 17. The BooleanLock Utility

IN THIS CHAPTER

The BooleanLock class, which I present in this chapter, provides a useful encapsulation of a boolean variable that is easily and safely accessed from multiple threads. These threads can test and set the internal value and wait for it to change. The wait/notify mechanism is used internally to support waiting for the value to change, and frees external classes from the error-prone complexity of properly implementing this mechanism.

Background

Using the wait/notify mechanism effectively and correctly requires discipline. It is easy to erroneously ...

Get Java Thread Programming now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.