BZ #162: Code can sometimes see uninitialized static fields

Status fields:

creation_ts:2011-11-19 22:45
component:vm
version:default branch
rep_platform:All
op_sys:All
bug_status:RESOLVED
resolution:FIXED
reporter:stefan@complang.tuwien.ac.at
The following program demonstrates this.

import java.util.concurrent.Semaphore;

class M {
    public static Semaphore sem = new Semaphore(0);
}

class testglobal {
    public static String the_string = initializeString();

    static String initializeString() {
        M.sem.release();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hullabaloo";
    }
}

public class main {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    M.sem.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("after acquire");
                System.out.println(String.valueOf(testglobal.the_string.length()));
            }
        }).start();
        System.out.println(testglobal.the_string);
    }
}

It fails most of the time with my GNU classpath build and occasionally with my OpenJDK
build, where "failing" means that a NPE is thrown.

Comment #1 by stefan@complang.tuwien.ac.at on 2011-11-30 00:46:40

http://mips.complang.tuwien.ac.at/hg/cacao/rev/89f8bfc577c7