Second Lombok Synchronized Example

Here is the second example of a program that deadlocks, with an example that I refactored using the @Synchronized annotation from Project Lombok.

This example was posted on one of the LinkedIn groups in which I first started asking about concurrency using the synchronized keyword. The basic idea to prevent deadlock is to mark the methods with the @Synchronized annotation. For one of them you should create a private, final object, and use the name of that object as the element for that annotation. For the other method that deadlocks I just use the annotation without an element.

Here is the original that was posted:

Here is the version with the Lombok refactoring:

I have a couple more examples before I get to my notes from No Fluff Just Stuff.

 

Image from the Wikipedia page for the Indonesian province of West Java, assumed allowed under Fair Use.