-
Maven - POM Configuration
Before you can use the java concurrent API, you need to add some dependencies to your "pom.xml" file.
Here's the minimal dependencies needed in order for the following examples to work:
-
Problem
First we will look at this example:
The problem with this example is if multiple threads call this method,
the value of the instance variable "id" that we print at the end of the method can be different from the value of the parameter "localId".
If 5 concurrent threads call this method, the output could be something like the following:
-
Using ReentrantLock to solve this problem
Output: