Annotations

Learn the use of various annotations used in concurrent code.

If you are interviewing, consider buying our number#1 course for Java Multithreading Interviews.

Overview

In this lesson, we’ll explore the various Java annotations that are related to concurrency. These are:

  1. @ThreadSafe
  2. @NotThreadSafe
  3. @Immutable
  4. @GuardedBy(lock)

Class-level annotations

Note that these annotations serve as documentation about class behavior but don’t change the ability or functionality of class in any way. Also, these class-level annotations become part of the public documentation of a class.

@ThreadSafe

The annotation @ThreadSafe can be applied to a class to indicate to users and maintainers that the class is thread safe and multiple threads can concurrently interact with an instance of the class without worrying about synchronization. Newbies should not confuse the annotation to mean that it makes a class thread safe. The ...