@Override
    public int drainAll(LongConsumer consumer) {
        int count = writeIndex - readIndex;
        if (count == 0) {
            return 0;
        }
        long value;
        while (!LOCK_UPDATER.compareAndSet(this, 0, 1)) {
        }
        try {
            int localReadIndex = readIndex;
            count = writeIndex - localReadIndex;
            long[] localValues = values;
            for (int i = 0; i < count; i++) {
                value = (long) VALUES_HANDLE.getVolatile(localValues, localReadIndex);
                consumer.accept(value);
                localReadIndex = (localReadIndex + 1) & (localValues.length - 1);
            }
            readIndex = localReadIndex;
            return count;
        } finally {
            LOCK_UPDATER.set(this, 0);
        }
    }
The lock is used to ensure the resize operation and the drain operation won't happen
simultaneously.
turms-server-common/src/main/java/im/turms/server/common/infra/collection/SpscGrowableLongRingBuffer.java