Link Details

Link 858437 thumbnail
User 478055 avatar

By mitchp
via java.dzone.com
Published: Oct 04 2012 / 11:40

I have often read articles saying "If an object is immutable, it is thread safe". But I have never found an article that convinces me that 'immutable' means thread safety. Even the book by Brian Goetz Java Concurrency in Practice with its chapter on immutability did not fully satisfiy me. In this book we can read word for word, in a frame : Immutable objects are always thread-safe. I think this sentence deserves more explanation. So I am going to try to define immutability and its relation to thread safety.
  • 6
  • 7
  • 907
  • 746

Comments

Add your comment
User 379963 avatar

lacayo replied ago:

0 votes Vote down Vote up Reply

I don't get your point, the objects in your example are mutable: their state changes and because of that there can be problems if two threads use them at the same time.

User 218789 avatar

eelmore replied ago:

-1 votes Vote down Vote up Reply

Of course immutable objects are thread safe. A value cannot be changed if the value cannot be changed, duh.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Apache Hadoop
Written by: Piotr Krewski
Featured Refcardz: Top Refcardz:
  1. Play
  2. Akka
  3. Design Patterns
  4. OO JS
  5. Cont. Delivery
  1. Play
  2. Java Performance
  3. Akka
  4. REST
  5. Java