java - How does ArrayBlockingQueue avoid shuffling array elements? -


scenario: producer fills array up, capacity new int[10], before consumer gets chance consume any. producer sees array full , blocks.

then consumer comes along , removes int[0], , signals producer array has empty slot fill.

my producer wakes up, , tries add new element array. considering int[0] free, , implementing fifo, arrayblockingqueue shuffle remaining 9 elements left, filling 0-8 indexes , leave int[9] free producer?

i've looked @ implementation don't see array copy functionality,

no copying of array elements performed, because arrayblockingqueue uses array circular buffer. maintaining 2 indexes, takeindex , putindex, , wraps them around when reach end of array.

after operation adds or takes element calls private "increment" method called inc, wraps index around end:

final int inc(int i) {     return (++i == items.length)? 0 : i; } 

here example of how method used:

private void insert(e x) {     items[putindex] = x;     putindex = inc(putindex); // <<== wraps around     ++count;     notempty.signal(); } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -