Race conditionsoccur only if multiple threads are accessing the same resource,andone or more of the threadswriteto the resource. If multiple threads read the same resourcerace conditionsdo not occur.

We can make sure that objects shared between threads are never updated by any of the threads by making the shared objects immutable, and thereby thread safe. Here is an example:

public class ImmutableValue{

  private int value = 0;

  public ImmutableValue(int value){
    this.value = value;
  }

  public int getValue(){
    return this.value;
  }
}

Notice how the value for theImmutableValueinstance is passed in the constructor. Notice also how there is no setter method. Once anImmutableValueinstance is created you cannot change its value. It is immutable. You can read it however, using thegetValue()method.

If you need to perform operations on theImmutableValueinstance you can do so by returning a new instance with the value resulting from the operation. Here is an example of an add operation:

public class ImmutableValue{

  private int value = 0;

  public ImmutableValue(int value){
    this.value = value;
  }

  public int getValue(){
    return this.value;
  }



      public ImmutableValue add(int valueToAdd){
      return new ImmutableValue(this.value + valueToAdd);
      }


}

Notice how theadd()method returns a newImmutableValueinstance with the result of the add operation, rather than adding the value to itself.

The Reference is not Thread Safe!

It is important to remember, that even if an object is immutable and thereby thread safe, the reference to this object may not be thread safe. Look at this example:

public class Calculator{
  private ImmutableValue currentValue = null;

  public ImmutableValue getValue(){
    return currentValue;
  }

  public void setValue(ImmutableValue newValue){
    this.currentValue = newValue;
  }

  public void add(int newValue){
    this.currentValue = this.currentValue.add(newValue);
  }
}

TheCalculatorclass holds a reference to anImmutableValueinstance. Notice how it is possible to change that reference through both thesetValue()andadd()methods. Therefore, even if theCalculatorclass uses an immutable object internally, it is not itself immutable, and therefore not thread safe. In other words: TheImmutableValueclass is thread safe, but theuse of itis not. This is something to keep in mind when trying to achieve thread safety through immutability.

To make theCalculatorclass thread safe you could have declared thegetValue(),setValue(), andadd()methodssynchronized. That would have done the trick.

results matching ""

    No results matching ""