2

I was reading about Immutable objects in java.

There is a statement which states - "Immutable objects are thread-safe". I need more clarification for the above statement:

If I have a shared resource of type 'String' which is shared with multiple threads (say 3) And if one of the thread make changes to the shared reference, it will create a new String object and that will available only with that Thread object and other threads will not get to know about the changes made by one of the thread.

Will it not lead to data inconsistency?

So could anyone please help me understand this?

Thanks in advance.

  • https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Jacek Cz Dec 07 '17 at 18:47
  • 4
    If there's a "shared reference" that you can "make changes" to, then it's not the _object_ that's not thread safe, but the _reference_ that might be unsafe. That doesn't mean the object isn't safe. – Louis Wasserman Dec 07 '17 at 18:48

2 Answers2

0

An object of an immutable class is always thread-safe. However, the reference to such an object doesn't have anything to do with that. It might be or it might not be thread-safe, depending on how the class that contains the reference to this thread-safe object (a String object, for example) and the methods to manipulate such a reference.

See this example below, to illustrate the explanation:

public class User {
   private string name;

   public User(String name) {
      this.name=name;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name=name;
   }
}

So, even though String is an immutable and thread-safe class, the reference to it in the class User above isn't thread-safe because its methods could be concurrently changing this reference to new String objects.

If you wanted it to be thread-safe, there would be many options. One of them would be to set all methods that can change the name object as synchronized.

Filipe Fedalto
  • 2,540
  • 1
  • 18
  • 21
0

Immutable types (in any language) are special reference types. Their uniqueness lies in the fact that when they are changed they are replaced with an entirely new instance which reflects the previous state + the change

Lets say 2 threads running a function which receive an immutable reference type object as a parameter ( and of course i'll use string for this example)

// pseudo code !!

  main() 
  {
      var str = "initialState";  
      new Thread(Do,str,1).Start();
      new Thread(Do,str,2).Start();
  }

  void Do(string arg,int tid)
  {
      int i = 0; 
      while(true)
      {
          arg += "running in thread " + tid + "for the " + i + "time";
          Console.WriteLine(arg);
      }
   }

This program while print in parallel the number of occurrences running in each thread with out effecting what was printed in the other thread.

Why ?

If you think about it string is a reference type, it is passed by reference and not by value, meaning it is not copied only a copy of the reference occurs. And yet though it was initially the same object. after the first change it is no longer and a different one is created. If it was still the same object after changes it would be a shared resource between the 2 threads and will disrupt the state those threads men't to print.

eran otzap
  • 12,293
  • 20
  • 84
  • 139