Can a separate thread change static variable?

Viewed 1559

After surfing the web, I am still confused about the following thread behavior. I am aware that static variables are shared within the same classloader, however there's sth definitely missing in this extract:

public class parallelCounter {
    public static final int N = 100000000;
    public static int j = 0;
    public static void inc() {
        for (int i = 0; i < N; i++) {
            j++;
        }
        System.out.println(j); // 10000000
    }
}

class parallelCounterDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                parallelCounter.inc();
            }
        });
        t1.start();
        System.out.println(parallelCounter.j); // 0 Why?
    }
}
5 Answers
Related