Effective Java: Prefer Concurrency Utilities Over wait and notify

Kyle Carter
Geek Culture
Published in
6 min readOct 26, 2021

--

Photo by Phil Hearing on Unsplash

At the core of each Object in the Java language there are three methods, wait, notify, and notifyAll. These methods allow you low-level concurrency control options. Up until Java 5, this was the go-to option for facilitating concurrency control. However, since the release of Java 5 (in 2004) there are now higher-level tools that can be used that are much easier and less error-prone. This being the case, in new code, we should be using exclusively these provided concurrency utilities.

The java.util.concurrent provides three different kinds of utilities. The first is the Executor framework discussed in a previous item, concurrent collections implementations, and synchronizers.

The concurrent collections provided by the Java language are high-performance concurrent implementation of the common collection interfaces (List, Queue, and `Map). These collections bring their own concurrency controls and thus don't need (and shouldn't be) externally synchronized. At times these collections provide lock-free implementations of functions and the fact that the user of the collection doesn't need to know how the implementation works is great.

Because these concurrent collections sometimes need to do multiple actions (because they are state-dependent) atomically they provide functions to…

--

--

Kyle Carter
Geek Culture

I'm a software architect that has a passion for software design and sharing with those around me.