Prasad
Prasad

Reputation: 90

Whether an object can be said thread-safe or not?

Is it safe to say that an object is thread-safe in Java if its class contains no instance variables that can be changed and no static variables?

Upvotes: 3

Views: 69

Answers (1)

Jean Logeart
Jean Logeart

Reputation: 53819

Totally safe, as long as it does not extend a non thread-safe class.

If an object is stateless, it can safely be shared by several threads.

That is also why it is encouraged to use immutable objects in multi-threaded environment as their state cannot be concurrently modified.

Upvotes: 4

Related Questions