I'm analyzing the LongAdder algorithm in detail. LongAdder extends the class Striped64 and in that class the essential method is retryUpdate. The following piece of code is taken from this method; in the linked source code it occupies lines 212–222:
try { // Recheck under lock
Cell[] rs; int m, j;
if ( (rs = cells) != null &&
(m = rs.length) > 0 &&
rs[j = (m - 1) & h] == null) {
rs[j] = r;
created = true;
}
} finally {
busy = 0;
}
Question: How can this try block fail?
Note that the array access
rs[j = (m - 1) & h]
shouldn't throw an IndexOutOfBoundsException because the result of a bitwise-and operation is always less than or equal than the minimum of its integer arguments, hence 0 <= j <= m-1 is within the bounds of the array.