Search code examples
javaconcurrencyguava

Will the number of stripes in Striped.lazyWeakReadWriteLock(int stripes) stripes grow as needed?


Trying to decide number of stripes when calling Striped.lazyWeakReadWriteLock(int stripes)

Javadoc says: @param stripes the minimum number of stripes (locks) required

Why does it say minimum in the javadoc? This lead me to believe that number of stripes is just the initial capacity and it will grow as needed and shrink back to the minimum number set at construction. Looking at code however it seems that the number of stripes is fixed.

Should I be doing: Striped.lazyWeakReadWriteLock(maximum_number_of_stripes) Or Striped.lazyWeakReadWriteLock(10) and expect it to grow?

I already wrote unit test and that confirmed that it has to be Striped.lazyWeakReadWriteLock(maximum_number_of_stripes)

So question is more about trying to interpret the javadoc. Why does it say minimum?


Solution

  • It's the minimum because the implementation allocates a fixed number of stripes that is at least that number.

    In particular, if you look at the implementation, it is rounded up to a power of two to make it particularly efficient to compute which stripe a given hash is mapped to.