Final Fields Semantics in Threads

馋奶兔 提交于 2019-12-03 15:11:07
Favonius

What you are looking for

What you are trying to test is called Don't publish the "this" reference during construction or Visibility Hazard. Read the following links in the order they are provided.

Reading

  1. Java theory and practice: Safe construction techniques
  2. JSR 133 (Java Memory Model) FAQ
  3. Do the ‘up to date’ guarantees for values of Java's final fields extend to indirect references?

Sample Code

class FinalField
{
    final int x;
    int y;

    public FinalField()
    {
        Thread t = new Thread(new TestThread(this));
        t.start();

        y = 4;
        x = 3;
    }
}

class TestThread implements Runnable
{
    FinalField f;
    TestThread(FinalField f)
    {
        if(f.x != 3)
            System.out.println("value of x = " + f.x);

        this.f = f;
    }

    public void run() 
    {
        if(f.x != 3)
            System.out.println("value of x = " + f.x);
    }
}

public class Test
{
    public static void main(String[] args) 
    {
        for(int i=0; i<100; i++)
        {
            new FinalField();
        }
    }
}

Output

value of x = 0
value of x = 0
value of x = 0
.
.
.
value of x = 0
value of x = 0
value of x = 0

Explanation of the output

When I access the final field in the constructor of my thread then at that time the final field x was not properly initialized, and that's why we are getting 0. Whereas when I access the same field in the run() then by that time the final field x is initialized to 3. This is happening because of the escaping of the reference to the object of FinalField. Read the 1st link I have shared, it is much more detailed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!