Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable objects are thread safe, but why?

Tags:

java

Lets say for example, a thread is creating and populating the reference variable of an immutable class by creating its object and another thread kicks in before the first one completes and creates another object of the immutable class, won't the immutable class usage be thread unsafe?

Creating an immutable object also means that all fields has to be marked as final.

it may be necessary to ensure correct behavior if a reference to a newly created instance is passed from one thread to another without synchronization

Are they trying to say that the other thread may re-point the reference variable to some other object of the immutable class and that way the threads will be pointing to different objects leaving the state inconsistent?

like image 219
Taran Singh Avatar asked Feb 15 '12 23:02

Taran Singh


2 Answers

Actually immutable objects are always thread-safe, but its references may not be.

Confused?? you shouldn't be:-

Going back to basic: Thread-safe simply means that two or more threads must work in coordination on the shared resource or object. They shouldn't over-ride the changes done by any other thread.

Now String is an immutable class, whenever a thread tries to change it, it simply end up creating a new object. So simply even the same thread can't make any changes to the original object & talking about the other thread would be like going to Sun but the catch here is that generally we use the same old reference to point that newly created object.

When we do code, we evaluate any change in object with the reference only.

Statement 1: String str = "123"; // initially string shared to two threads

Statement 2: str = str+"FirstThread"; // to be executed by thread one

Statement 3: str=str+"SecondThread"; // to be executed by thread two

Now since there is no synchronize, volatile or final keywords to tell compiler to skip using its intelligence for optimization (any reordering or caching things), this code can be run in following manner.

  1. Load Statement2, so str = "123"+"FirstThread"
  2. Load Statement3, so str = "123"+"SecondThread"
  3. Store Statement3, so str = "123SecondThread"
  4. Store Statement2, so str = "123FirstThread"

and finally the value in reference str="123FirstThread" and for sometime if we assume that luckily our GC thread is sleeping, that our immutable objects still exist untouched in our string pool.

So, Immutable objects are always thread-safe, but their references may not be. To make their references thread-safe, we may need to access them from synchronized blocks/methods.

like image 195
amandeep1991 Avatar answered Sep 28 '22 17:09

amandeep1991


In addition to other answers posted already, immutable objects once created, they cannot be modified further. Hence they are essentially read-only.

And as we all know, read-only things are always thread-safe. Even in databases, multiple queries can read same rows simultaneously, but if you want to modify something, you need exclusive lock for that.

like image 23
Bhushan Avatar answered Sep 28 '22 17:09

Bhushan