Re: Using Enumerated Types as Array Indexes
On 8/17/2011 7:09 PM, markspace wrote:
On 8/17/2011 10:02 AM, Robert Klemme wrote:
1. There was an AtomicInteger initialized at startup with 0 which for
every request coming into that system was incremented in a thread safe
manner (incrementAndGet()).
I'd be shocked if an int actually overflowed when counting requests like
this, but that's all I can think of. 2^31 is such a huge number, I
didn't think you could get there in practical cases.
He *did* write "two months of uninterrupted [...] usage." Two
months ~= 60 days ~= 5 megaseconds, so a 2G counter overflows if it
increments at ~400 Hz or more, give or take a few backs of envelopes.
Assuming that the AtomicInteger did overflow, then n % y produces
negative numbers if n is negative, a clear AIOOB.
Try : arr[ Math.abs( n ) % arr.length ];
Bzzzzt! Thank you for playing, go back to the rear of the line.
While awaiting your next opportunity, ponder: What is the sign of
Math.abs(Integer.MIN_VALUE)?
--
Eric Sosman
esosman@ieee-dot-org.invalid