@ThreadSafe
public class A
{
}
Does this annotation actually make the class Thread Safe or is it just for readability?
Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data. When no sharing is intended, give each thread a private copy of the data.
Using Atomic Variable Using an atomic variable is another way to achieve thread-safety in java. When variables are shared by multiple threads, the atomic variable ensures that threads don't crash into each other.
thread-safety or thread-safe code in Java refers to code that can safely be utilized or shared in concurrent or multi-threading environment and they will behave as expected.
Immutable arrays (declared using let) are thread-safe since it is read-only. But mutable arrays are not thread-safe.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With