Third Lombok Synchronized Example

Here is another installment of my exploration into concurrency in Java. I have written about finding the Lombok library which uses annotations to reduce boilerplate in Java code.

One of the annotations provided by Lombok is @Synchronized. It is to replace the “synchronized” keyword and prevent some of the problems that can come with using the “synchronized” keyword. There are other annotations available via Lombok, but I have not looked at them.

Here is the third example of a program with a deadlock using the “synchronized” keyword that I refactored with Lombok. I got this example from the Java 2S site.

For this one I have also included the output of each of the programs.

Here is the program:

Here is its output:

There is a line that says “Ending method main”, but it does not end unless you kill the program.

Here is the program refactored with Lombok:

Here is the output of the refactored program:

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