Medium java spring

Two threads print 1–100 (odd/even) with no synchronization?

The 30 second answer

Ordering is not guaranteed, so the output can interleave in any order. If they also share a counter without synchronization you get a data race: lost updates, duplicates or skips, and visibility bugs under the Java Memory Model. I fix it with a shared lock and wait/notify on a turn condition, two Conditions on a ReentrantLock, Semaphores that hand off, or an AtomicInteger turn check for a demo.

Ordering is not guaranteed: output interleaves arbitrarily (e.g. 1,3,5,2,…). If they share a counter without synchronization you also get a data race — lost updates, duplicates/skips, and visibility problems (a thread may not see the other's write; the JMM gives no happens-before). Fix: shared lock + wait/notifyAll on a turn condition, ReentrantLock with two Conditions, two Semaphores handing off, or an AtomicInteger with turn check (busy-wait; fine for demo only).

Sources:

Sources (0)
See inline citations in the answer above.