We saw how immutability affects the creation and design of programs, so now we will understand how it is useful.
In this section, we will cover the following topics:
- The cons of mutable collections
- Creating two threads that simultaneously modify a mutable collection
- Reasoning about a concurrent program
Let's first understand the cause of mutable collections. To do this, we will be creating two threads that simultaneously modify the mutable collection. We will be using this code for our test. First, we will create a ListBuffer that is a mutable list. Then, we can add and delete links without creating another list for any modification. We can then create an Executors service with two threads. We need two threads to start simultaneously to modify the state. Later, we will use a CountDownLatch construct from Java.util.concurrent...