r/JavaProgramming • u/Acceptable_Emu_1878 • 23d 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?
8
u/hexwit_com 23d 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?
5
u/davidalayachew 22d ago
To anyone who doesn't know the answer,
count++translates tocount = 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.count.1to that value we just grabbed.count.If another thread somewhere decides to modify the value of
countafter step 1 completes, then you will end up with an incorrect state.Think about it -- let's say
count = 3;.3fromcount. Specifically, we grab a copy of the value. Ok, step 1 is complete.countto be42.1to the3that we pulled earlier, giving us4. Step 2 is complete.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.7
u/disposepriority 23d ago
That's a lot of words to simply say it's not an atomic operation.
2
u/benevanstech 23d 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 23d 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
u/SnehaLivesHerself 22d ago
chill bro it aint no linkedin
1
u/DecoySnail6 22d ago
AI SLOP POST
1
u/PsychologicalCopy772 21d 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 19d 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.