r/JavaProgramming • u/Acceptable_Emu_1878 • 22d ago
We know count++ isn’t thread-safe. But do you know why?
I conducted a mock interview recently and one thing stood out to me.
I asked a Java question related to concurrency:
class Counter {
private int count = 0;
public void increment() {
count++;
}
}“Is this thread-safe?”
The candidate said "No".
So I asked, “Why?”
He knew that it wasn't thread-safe, but explaining exactly why was difficult. He then said we need to synchronise the method.
So I asked:
“How does that fix it?” and then “What exactly is being locked here?”
That was where the interview got interesting.
He knew the terms — synchronised, volatile, race condition, etc. However, connecting them and explaining what was actually happening between the threads was harder.
It made me think about how easy it is to prepare for interviews by remembering rules.
count++ → not thread-safe → use synchronised
But the moment someone asks “Why?”, we need to understand what is actually happening.
I think that's something that's easy to miss while preparing for interviews. We can keep solving questions and memorising concepts, but it's worth stopping sometimes and asking ourselves:
“Can I actually explain why this works?” and “What happens if I change one thing?”
How do you guys prepare for the “Why?” part of interviews?
5
u/davidalayachew 22d ago
To anyone who doesn't know the answer, count++ translates to count = count + 1, and that is not a thread-safe operation.
To explain it simply (but with more words), there are 3 things that happen in count = count + 1.
We grab the value of
count.We add
1to that value we just grabbed.We store that newly summed value to
count.
If another thread somewhere decides to modify the value of count after step 1 completes, then you will end up with an incorrect state.
Think about it -- let's say count = 3;.
Well, step 1 says that we extract
3fromcount. Specifically, we grab a copy of the value. Ok, step 1 is complete.Next, some random thread jumps in and modifies the value of
countto be42.While that is happening, we add
1to the3that we pulled earlier, giving us4. Step 2 is complete.Now, we write
4tocount, causing an incorrect state. We wanted43, but got4. Hell,42would have been a valid answer, but that's not what we ended up with.
1
u/Necessary_Apple_5567 21d ago
Answer is more complicated than that. The value of the country high likely will be cached in the process cache, do, even if count++ completes atomically, two versions of the counter can be updated and flushed back go memory which can be problematic in two places: 1) update of the outdated version 2) overwrite result of the racing thread
7
2
u/benevanstech 22d ago
For this specific question, it's actually easier to see what's going on by looking at the bytecode (which also explains why volatile doesn't help).
There's a full treatment in my book: "The Well-Grounded Java Developer (2nd Ed)" which might be a good text for interview prep, as it focuses on topics like concurrency and functional programming - and tries to explain the *why* as much as possible.
1
u/Spicy_O_licorice 22d ago
I just had hit the same wall where the right answer was not enough
They asked me what's the recommended way To do dependency injection in spring boot? I answer Why is it?
Apparently cuz the doc said so was not the answer
1
u/Afonso2002 22d ago
I think looking at assembly is important. Both thread can read same value to cpu register.
Both do inc. And after one write, the other will write above the value. And we lost an inc operation
1
1
1
u/PsychologicalCopy772 20d ago
can't we use java.util.concurrent.atomic and classes like AtomicInteger with methods addAndGet ?? If not please correct me.
1
u/Legitimate-Comment18 18d ago
It's because it's a read modify write operation. A thread might read an old / stale value of count and increment it from the old value.
8
u/hexwit_com 22d ago
We need to decide - ai replacing all of us and nobody need to know anything, or still we need engineers with full set of knowledge?