简体   繁体   中英

Java Object Construction In Multi-Threaded Environment

I am reading this book called "Java Concurrency in Practice" and the author gives an example of an unsafe object publication. Here is the example.

public Holder holder;

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

and

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

So does this mean that other thread has access to an object when it is not even fully constructed? I guess that when a thread A calls holder.initialize(); and thread B calls holder.assertSanity(); the condition n != n will not be met if thread A has not yet executed this.n = n;

Does this also mean that if I have a simpler code like

int n;

System.out.println(n == n); //false?

A problem can occur if the assertSanity method is pre-empted between the first and second load of n (the first load would see 0 and the second load would see the value set by the constructor). The problem is that the basic operations are:

  1. Allocate space for the object
  2. Call the constructor
  3. Set holder to the new instance

The compiler/JVM/CPU is allowed to reorder steps #2 and #3 since there are no memory barriers (final, volatile, synchronized, etc.)

From your second example, it's not clear if "n" is a local variable or a member variable or how another thread might be simultaneously mutating it.

Your understanding is correct. That is exactly the problem the author seek to illustrate. There are no guards in Java that ensure an object is fully constructed prior to accessing in when multiple threads are concerned. Holder is not thread-safe as it contains mutable state. The use of synchronization is required to fix this.

I'm not sure I understand your 2nd example, it lacks context.

public static void main(String[] args) {

    A a = new A();
    System.out.println(a.n);

}

static class A{
    public int n;

    public A(){

        new Thread(){

            public void run() {
                System.out.println(A.this.n);
            };

        }.start();

        try {
            Thread.currentThread().sleep(1000);
            n=3;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

This example result in "0 3" which means reference to an object could be used by another thread even before its constructor done. You may find the rest answer here . Wish it could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM