18
@ThreadSafe 
public class A
{
}

Does this annotation actually make the class Thread Safe or is it just for readability?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Phoenix
  • 8,695
  • 16
  • 55
  • 88
  • 5
    **Which** such class are you using? There's no class by that name in the JDK itself, so you'll need to give us the fully-qualified class name. And: chances are it does *not* actually make anything thread-safe as annotations themselves can't modify the behaviour or the class they are applied to (unless you *also* use some bytecode weaving, classloader magic or proxying). – Joachim Sauer Oct 02 '13 at 07:03
  • 15
    It would be great if thread safety were that easy, unfortunately it's not! – assylias Oct 02 '13 at 07:28
  • The annotation was imported from a package. That package has documentation. What does that documentation say? – Raedwald Oct 02 '13 at 07:34
  • SO question for more information about ["@GuardedBy, @ThreadSafe, @NotThreadSafe"](http://stackoverflow.com/questions/11362298/guardedby-threadsafe-notthreadsafe). – chrisjleu Mar 10 '15 at 07:40
  • Does this answer your question? [@GuardedBy , @ThreadSafe ,@NotThreadSafe](https://stackoverflow.com/questions/11362298/guardedby-threadsafe-notthreadsafe) – rds Jan 12 '21 at 17:58

2 Answers2

30

See @ThreadSafe Annotation:

Place this annotation on methods that can safely be called from more than one thread concurrently. The method implementer must ensure thread safety using a variety of possible techniques including immutable data, synchronized shared data, or not using any shared data at all.

It does not make the class Thread Safe, the programmer does it Thread Safe and adds the annotation.

You might want to see this helpful link too.

stkent
  • 19,772
  • 14
  • 85
  • 111
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    I'm sorry, but you're guessing. The two links that you posted refer to entirely different `@ThreadSafe` annotations and we don't know if the OP uses any of those. (Sidenote: using something more descriptive as "this" as the link-text would be highly encouraged). – Joachim Sauer Oct 02 '13 at 07:15
  • @JoachimSauer Thanks for your advice. I'll take your comment into consideration to improve my answers in the future. I don't know to which annotation he refers, that's why I suggested the other link. – Maroun Oct 02 '13 at 07:18
  • 1
    The link you provided was very helpful. Thank you. – vitrums May 29 '17 at 17:43
0

The @ThreadSafe annotation is used:

  • to express the thread safety guarantee to the users of the annotated class, so that they can quickly understand whether they should use this class in a multi-threaded environment. Perhaps more importantly, it gives them guarantee that this will also be true in the future.
  • for the maintainers of the code to be aware that their changes must not break the thread safety promise. This is important because client code may depend on this class being thread-safe.
  • by IDEs and static analysis tools to help you identify potential concurrency-related problems when using this class.

The annotation does not change the way the code itself operates, and putting it on a class that does not guarantee thread safety is a mistake.

flounder
  • 71
  • 1
  • 3