hollow/src/main/java/com/netflix/hollow/core/memory/ThreadSafeBitSet.java
private AtomicLongArray getSegment(int segmentIndex) {
        ThreadSafeBitSetSegments visibleSegments = segments.get();

        while(visibleSegments.numSegments() <= segmentIndex) {
            /// Thread safety:  newVisibleSegments contains all of the segments from the currently visible segments, plus extra.
            /// all of the segments in the currently visible segments are canonical and will not change.
            ThreadSafeBitSetSegments newVisibleSegments = new ThreadSafeBitSetSegments(visibleSegments, segmentIndex + 1, numLongsPerSegment);

            /// because we are using a compareAndSet, if this thread "wins the race" and successfully sets this variable, then the segments
            /// which are newly defined in newVisibleSegments become canonical.
            if(segments.compareAndSet(visibleSegments, newVisibleSegments)) {
                visibleSegments = newVisibleSegments;
            } else {
                /// If we "lose the race" and are growing the ThreadSafeBitSet segments larger,
                /// then we will gather the new canonical sets from the update which we missed on the next iteration of this loop.
                /// Newly defined segments in newVisibleSegments will be discarded, they do not get to become canonical.
                visibleSegments = segments.get();
            }
        }

        return visibleSegments.getSegment(segmentIndex);
    }
private AtomicLongArray getSegment(int segmentIndex) {
        ThreadSafeBitSetSegments visibleSegments = segments.get();

        while(visibleSegments.numSegments() <= segmentIndex) {
            /// Thread safety:  newVisibleSegments contains all of the segments from the currently visible segments, plus extra.
            /// all of the segments in the currently visible segments are canonical and will not change.
            ThreadSafeBitSetSegments newVisibleSegments = new ThreadSafeBitSetSegments(visibleSegments, segmentIndex + 1, numLongsPerSegment);

            /// because we are using a compareAndSet, if this thread "wins the race" and successfully sets this variable, then the segments
            /// which are newly defined in newVisibleSegments become canonical.
            if(segments.compareAndSet(visibleSegments, newVisibleSegments)) {
                visibleSegments = newVisibleSegments;
            } else {
                /// If we "lose the race" and are growing the ThreadSafeBitSet segments larger,
                /// then we will gather the new canonical sets from the update which we missed on the next iteration of this loop.
                /// Newly defined segments in newVisibleSegments will be discarded, they do not get to become canonical.
                visibleSegments = segments.get();
            }
        }

        return visibleSegments.getSegment(segmentIndex);
    }
private AtomicLongArray getSegment ( int segmentIndex ) { ThreadSafeBitSetSegments visibleSegments = segments . get ( ) ; while ( visibleSegments . numSegments ( ) <= segmentIndex ) { /// Thread safety:  newVisibleSegments contains all of the segments from the currently visible segments, plus extra. /// all of the segments in the currently visible segments are canonical and will not change. ThreadSafeBitSetSegments newVisibleSegments = new ThreadSafeBitSetSegments ( visibleSegments , segmentIndex + 1 , numLongsPerSegment ) ; /// because we are using a compareAndSet, if this thread "wins the race" and successfully sets this variable, then the segments /// which are newly defined in newVisibleSegments become canonical. if ( segments . compareAndSet ( visibleSegments , newVisibleSegments ) ) { visibleSegments = newVisibleSegments ; } else { /// If we "lose the race" and are growing the ThreadSafeBitSet segments larger, /// then we will gather the new canonical sets from the update which we missed on the next iteration of this loop. /// Newly defined segments in newVisibleSegments will be discarded, they do not get to become canonical. visibleSegments = segments . get ( ) ; } } return visibleSegments . getSegment ( segmentIndex ) ; }
Get the segment at <code>segmentIndex</code>.  If this segment does not yet exist, create it.

@param segmentIndex the segment index
@return the segment
Get the segment at <code > segmentIndex< / code > . If this segment does not yet exist create it .