Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation called ThreadSafe

Tags:

java

@ThreadSafe 
public class A
{
}

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

like image 341
Phoenix Avatar asked Oct 02 '13 06:10

Phoenix


People also ask

What is meant by thread-safe?

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.

How do you make a class thread-safe in Java?

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.

What is thread safety in Java?

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.

Is array thread-safe in Java?

Immutable arrays (declared using let) are thread-safe since it is read-only. But mutable arrays are not thread-safe.


1 Answers

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.

like image 199
Maroun Avatar answered Sep 17 '22 18:09

Maroun