10

What is the difference between thread safe and thread compatible?

  • What thread compatible mean?
  • What is use cases for thread compatible?

UPD: I have found this definition in the grpc documentation of StreamObserver.

Also, I have found the link to Characterizing thread safety but its still not clear for me.

If a method requires to be in synchronize block, that means that is just threaded unsafe?

mkUltra
  • 2,828
  • 1
  • 22
  • 47
  • 2
    Some context on where you even found the term "thread compatible" would be great, I've never even heard of it. I imagine the distinction would be how much the user would be involved in ensuring that the API is being used in a manner that would then be considered "thread-safe" –  Oct 09 '18 at 18:47
  • even I never heard this word "thread compatible". Can you post the complete context of the source ? – Ravi Oct 10 '18 at 07:22
  • I have updated the question with context information. – mkUltra Oct 10 '18 at 07:39
  • Looks like the term was coined in "Effective Java 1st edition" - see my answer below. – David Soroko Oct 12 '18 at 14:16
  • 1
    Here is another framework documentation that mentions this term, https://beam.apache.org/documentation/programming-guide/#requirements-for-writing-user-code-for-beam-transforms – Adelin Sep 05 '19 at 09:08

3 Answers3

8

Thread safe means that an object can be used by many threads concurrently and still be correct 1

Thread hostile means that the object does something (mutates static state, thread local storage etc.) that prevents it from being thread safe.

Thread compatible means not thread safe, but not thread hostile - so to satisfy thread safety, the user must perform synchronization themselves


1 But the definition of correctness varies a little...

Java In Theory And In Practice defines this according to the class's specification.

Geoff Romer at Google and Wikipedia define this as simply lack of data races.

I usually hope this to mean no crashes, deadlocks or other surprises.

Peter Wishart
  • 11,600
  • 1
  • 26
  • 45
1

thread safe means that the object is safe to use concurrently in multiple threads, it is implemented by java self; although thread compatiple is not thread safe, it still can be safe to use concurrently when you surround some synchronization code in your non-thread-safe code or have a wrapper object where it is thread safe.Namely that you shoud implement is by yourself.

sasa
  • 51
  • 2
0

It looks like the definion of thread compatible is due to Joshua Bloch's in Effective Java 1st edition. For whatever it's worth it is not present in the second edition. In the second edition we have the following taxonomy

  • immutable
  • unconditionally thread-safe
  • conditionally thread-safe
  • not thread-safe
  • thread-hostile

Looks to me that thread compatible was renamed to not thread-safe.

David Soroko
  • 8,521
  • 2
  • 39
  • 51
  • why do you think so? It looks to me it is renamed to `conditionally thread-safe`. The documentation of Beam uses the term, "thread-compatible" just like the meaning, `conditionally thread-safe`. (https://beam.apache.org/documentation/programming-guide/#requirements-for-writing-user-code-for-beam-transforms) – Jason Kim Aug 31 '21 at 12:53