Simultaneidade em Java usando blocos sincronizados, sem fornecer os resultados esperados

Abaixo está um programa java trivial. Ele possui um contador chamado "cnt" que é incrementado e adicionado a uma lista chamada "monitor". "cnt" é incrementado por vários threads e os valores são adicionados ao "monitor" por vários threads.

No final do método "go ()", cnt e monitor.size () devem ter o mesmo valor, mas não. monitor.size () tem o valor correto.

Se você alterar o código descomentando um dos blocos sincronizados comentados e comentando o atualmente não comentado, o código produzirá os resultados esperados. Além disso, se você definir a contagem de threads (THREAD_COUNT) como 1, o código produzirá os resultados esperados.

Isso só pode ser reproduzido em uma máquina com vários núcleos reais.

public class ThreadTester {

    private List<Integer> monitor = new ArrayList<Integer>();
    private Integer cnt = 0;
    private static final int NUM_EVENTS = 2313;
    private final int THREAD_COUNT = 13;

    public ThreadTester() {
    }

    public void go() {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                for (int ii=0; ii<NUM_EVENTS; ++ii) {
                    synchronized( monitor) {
                        synchronized(cnt) {        // <-- is this synchronized necessary?
                            monitor.add(cnt);
                        }
//                        synchronized(cnt) {
//                            cnt++;        // <-- why does moving the synchronized block to here result in the correct value for cnt?
//                        }
                    }
                    synchronized(cnt) {
                        cnt++;              // <-- why does moving the synchronized block here result in cnt being wrong?
                    }
                }
//                synchronized(cnt) {
//                    cnt += NUM_EVENTS;    // <-- moving the synchronized block here results in the correct value for cnt, no surprise
//                }
            }

        };
        Thread[] threads = new Thread[THREAD_COUNT];

        for (int ii=0; ii<THREAD_COUNT; ++ii) {
            threads[ii] = new Thread(r);
        }
        for (int ii=0; ii<THREAD_COUNT; ++ii) {
            threads[ii].start();
        }
        for (int ii=0; ii<THREAD_COUNT; ++ii) {
            try { threads[ii].join(); } catch (InterruptedException e) { }
        }

        System.out.println("Both values should be: " + NUM_EVENTS*THREAD_COUNT);
        synchronized (monitor) {
            System.out.println("monitor.size() " + monitor.size());
        }
        synchronized (cnt) {
            System.out.println("cnt " + cnt);
        }
    }

    public static void main(String[] args) {
        ThreadTester t = new ThreadTester();
        t.go();

        System.out.println("DONE");
    }    
}

questionAnswers(1)

yourAnswerToTheQuestion