← Quiz: Java Concurrency Utilities: all 10 questions
Quiz: Java Concurrency Utilities · Question 1 of 10
Why can two threads running count++ on a shared int lose updates?
Why can two threads running count++ on a shared int lose updates?
- A.Because int is too small
- B.Because count++ is a read, an add and a write, and the threads can interleave between them
- C.Because Java does not allow threads to share variables
- D.Because ++ is slower than += 1
Answer & explanation
Correct answer: Because count++ is a read, an add and a write, and the threads can interleave between them
If both threads read the same old value, both write the same new value and one increment disappears.
Can you get the rest right without peeking?
Take the timed Quiz: Java Concurrency Utilities quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →