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? } }
t1.join()(or wait) to expect any values calculated byinc()