3

Suppose you set up a custom annotation

public @interface ThreadSafe {
}

To indicate to the reader of your code that classes or methods are in fact Thread Safe

Other then a variant of a comment, how else can these types of custom annotations be used?

James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 1
    The answers here go a long way to answering your question: http://stackoverflow.com/questions/3341930/use-cases-for-implementing-annotations – ControlAltDel Jul 05 '12 at 19:43

2 Answers2

1

If you're just asking about potential applications of such an annotation, here's a few off the top of my head...

  • Reflective code might take advantage of the thread-safety of annotated methods. For example, Guava's EventBus uses an @AllowConcurrentEvents annotation as a hint for its EventBus framework.
  • Static analysis tools can use annotations as hints -- either that a method should have a certain property, or that users of this method can assume a certain property. For example, a static analysis tool might perform extra checks on methods annotated @ThreadSafe to make sure they are, in fact, thread safe.
  • You could design preprocessors that might identify optimization opportunities that are only available because a specific method is thread-safe.
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

Such annotations can bring information that could be used to improve the efficiency of algorithms. The RandomAccess interface is used for this purpose.

Benoit
  • 1,995
  • 1
  • 13
  • 18