I'm currently learning about threads in java, and from what I've read, implementing them isn't the problem so much as making your code thread safe. Which brings me to my question: should I be using immutable objects whenever possible to ensure that I prevent concurrency bugs?
I've read differing opinions about when to use mutable objects, including:
If immutable objects are good, why do people keep creating mutable objects? (Programmers.SE)
for large and/or complex objects, creating a new copy of the object for every single change can be very costly and/or tedious. And for objects with a distinct identity, changing an existing objects is much more simple and intuitive than creating a new, modified copy of it.
Immutable Objects (Java Tutorials)
The impact of object creation is often overestimated, and can be offset by some of the efficiencies associated with immutable objects. These include decreased overhead due to garbage collection, and the elimination of code needed to protect mutable objects from corruption.
So, is there a best practice when implementing threads? Should I always try to use immutable objects when possible?