Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are immutable objects?

What is the relationship with thread-safety and immutable objects? Does it makes easier to share a single resource among multiple threads? If immutable objects are stateless, can they be pooled in a container like a J2EE container?

thanks

like image 374
Dunith Dhanushka Avatar asked Jan 19 '10 17:01

Dunith Dhanushka


1 Answers

Immutable objects are objects that can not be changed. If an object can not be changed, then there is no concern that a competing thread will change the object state "behind the back" of the executing thread, and therefore immutable objects do not need to be protected via synchronization or some other technique.

like image 107
Will Hartung Avatar answered Oct 21 '22 13:10

Will Hartung