Grokking Java Interview Questions
Vote

0% completed

​

Race Conditions and synchronized

Two threads each add 1 to the same int counter 1,000,000 times, so the counter should end at 2,000,000. We repeated this five times within one program on Java 21, and the final values were 1,877,287, 1,874,668, 1,274,553, and 2,000,000 twice. The worst attempt lost more than 725,000 additions, and two attempts looked correct.

Four questions follow from these numbers: how an update disappears, which object a synchronized method locks, why a thread can take the same lock twice, and how much code one lock should cover.

What is a race condition?

Level: Junior

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content