I don't get it. debugSession is never changed in this code So it will be the same for all the threads all the time. Why do we need to mark it as volatile? O course debugSession has a method to change it but the program doesn't use it a single time.
In fact, I would mark rather aliveThreadIndex as volatile, because it might be incremented by all threads.... but of course, I can't because of validation that says: "3. The Solution class must have a private static final AtomicInteger aliveThreadIndex field." (volatile and final can not be combined).
Can anyone explain it to me?
Contemplation failed
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Jurij Thmsn
23 November 2021, 14:20
Wondering about the same. Did you find an answer?
0
Lisa
23 November 2021, 14:37
aliveThreadIndex is an AtomicInteger and those are volatile by definition. The debugSession variable I've explained to myself the same way Jakub did. It is prepared and intended to switch, maybe from another task we're not able to see atm.
+2
Jurij Thmsn
23 November 2021, 14:40
Thanks for your quick answer!
0