0
@ThreadSafe
public interface ServerInterceptor {
    ...
}

I am developing a gRPC server.

I found that the interface ServerInterceptor is annotated with @ThreadSafe. I am confused about this annotation when I want to implement ServerInterceptor.

Does it mean I need to ensure thread safety of implementation?

Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
gzc
  • 8,180
  • 8
  • 42
  • 62
  • 2
    Possible duplicate of [Annotation called ThreadSafe](https://stackoverflow.com/questions/19131094/annotation-called-threadsafe) – Ori Marko Feb 13 '19 at 09:21
  • this question may what you want[What is meant by “thread-safe” code?](https://stackoverflow.com/questions/261683/what-is-meant-by-thread-safe-code) – TongChen Feb 13 '19 at 09:23
  • @TongChen I know "thread-safe", what I don't understande is how to implement interface annotated with jsr305 ThreadSafe. – gzc Feb 13 '19 at 09:28
  • @gzc this is annotation just a hint,the coder must guarantee this method is thread safe.how to make a thread safe this [What Makes a Method Thread-safe? What are the rules?](https://stackoverflow.com/questions/9848067/what-makes-a-method-thread-safe-what-are-the-rules) may help you. – TongChen Feb 13 '19 at 09:53
  • 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

3

@ThreadSafe is just an indication to other programmers using/implementing the interface/class that it SHOULD be thread safe. Doesn't guarantee anything however.

Pete
  • 205
  • 2
  • 14
2

By annotating a class/interface with annotation @ThreadSafe, it doesn't mean that the class is thread safe. Programmer need to make the class as thread safe and than annotate the class with this annotation.

In your case by annotation the interface ServerInterceptor with @ThreadSafe probably mean that, the person who will be writing the implementation class for this interface have to make that class as thread safe.

So this just for readability and to convey a message to colleague program that what the person had in mind while writing this interface.

Deepak Kumar
  • 1,246
  • 14
  • 38
  • 1
    That's correct. In addition, it's possible that a code analysis tool (such as IntelliJ IDEA or Findbugs) might (possibly in a future enhancement if it doesn't already) issue some warning if it detects non-threadsafe behaviour in a class thus annotated. – DodgyCodeException Feb 13 '19 at 09:50