Synchronized with Project Lombok

To avoid multiple threads using a method at the same time, we can declare the method using synchronized keyword of Java like below:

Project Lombok also provides a safer way to do this by using @Synchronized annotation.

With this annotation, Lombok will generate a private field named $lock for nonstatic method and a private field named $LOCK for static method. This will be safer way because it will allow use lock on an instance field rather than on this.

If you check the Example.class file in /target/classes/com/huongdanjava/lombok, you will see the content as below:

Add Comment