Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not thread-safe Object publishing

Reading "Java Concurrency In Practice", there's this part in section 3.5:

public Holder holder;
public void initialize() {
     holder = new Holder(42);
}

Besides the obvious thread safety hazard of creating two instances of Holder, the book claims a possible publishing issue can occur.

Furthermore, for a Holder class such as

public Holder {
    int n;
    public Holder(int n) { this.n = n };
    public void assertSanity() {
        if(n != n)
             throw new AssertionError("This statement is false.");
    }
}

an AssertionError can be thrown!

How is this possible? The only way I can think of that can allow such ridiculous behavior is if the Holder constructor would not be blocking, so a reference would be created to the instance while the constructor code still runs in a different thread.

Is this possible?

like image 516
Maxim Veksler Avatar asked Oct 25 '09 17:10

Maxim Veksler


People also ask

Which class is not a thread-safe?

When designing a class that may be used for concurrent programming—that is, a class whose instances may be used by more than one thread at a time—it is imperative that you make sure the class is " thread-safe.” Consider the IntList class of Example 2-7. This class is not thread safe.

What is a thread-safe object?

A thread-safe object is one that always maintains a valid state, as observed by other classes and objects, even in a multithreaded environment.

How can you tell if an object is thread-safe?

A method will be thread safe if it uses the synchronized keyword in its declaration.

What is safe publication?

Safe publication makes all the values written before the publication visible to all readers that observed the published object. It is a great simplification over the JMM rules of engagement with regards to actions, orders and such.


3 Answers

The reason why this is possible is that Java has a weak memory model. It does not guarantee ordering of read and writes.

This particular problem can be reproduced with the following two code snippets representing two threads.

Thread 1:

someStaticVariable = new Holder(42);

Thread 2:

someStaticVariable.assertSanity(); // can throw

On the surface it seems impossible that this could ever occur. In order to understand why this can happen, you have to get past the Java syntax and get down to a much lower level. If you look at the code for thread 1, it can essentially be broken down into a series of memory writes and allocations:

  1. Alloc memory to pointer1
  2. Write 42 to pointer1 at offset 0
  3. Write pointer1 to someStaticVariable

Because Java has a weak memory model, it is perfectly possible for the code to actually execute in the following order from the perspective of thread 2:

  1. Alloc Memory to pointer1
  2. Write pointer1 to someStaticVariable
  3. Write 42 to pointer1 at offset 0

Scary? Yes but it can happen.

What this means though is that thread 2 can now call into assertSanity before n has gotten the value 42. It is possible for the value n to be read twice during assertSanity, once before operation #3 completes and once after and hence see two different values and throw an exception.

EDIT

According to Jon Skeet, the AssertionError migh still occur with Java 8 unless the field is final.

like image 58
JaredPar Avatar answered Oct 23 '22 07:10

JaredPar


The Java memory model used to be such that the assignment to the Holder reference might become visible before the assignment to the variable within the object.

However, the more recent memory model which took effect as of Java 5 makes this impossible, at least for final fields: all assignments within a constructor "happen before" any assignment of the reference to the new object to a variable. See the Java Language Specification section 17.4 for more details, but here's the most relevant snippet:

An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields

So your example could still fail as n is non-final, but it should be okay if you make n final.

Of course the:

if (n != n)

could certainly fail for non-final variables, assuming the JIT compiler doesn't optimise it away - if the operations are:

  • Fetch LHS: n
  • Fetch RHS: n
  • Compare LHS and RHS

then the value could change between the two fetches.

like image 26
Jon Skeet Avatar answered Oct 23 '22 06:10

Jon Skeet


Well, in the book it states for the first code block that:

The problem here is not the Holder class itself, but that the Holder is not properly published. However, Holder can be made immune to improper publication by declaring the n field to be final, which would make Holder immutable; see Section 3.5.2

And for the second code block:

Because synchronization was not used to make the Holder visible to other threads, we say the Holder was not properly published. Two things can go wrong with improperly published objects. Other threads could see a stale value for the holder field, and thus see a null reference or other older value even though a value has been placed in holder. But far worse, other threads could see an up-todate value for the holder reference, but stale values for the state of the Holder.[16] To make things even less predictable, a thread may see a stale value the first time it reads a field and then a more up-to-date value the next time, which is why assertSanity can throw AssertionError.

I think JaredPar has pretty much made this explicit in his comment.

(Note: Not looking for votes here -- answers allow for more detailed info than comments.)

like image 1
alphazero Avatar answered Oct 23 '22 06:10

alphazero