Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package dev.mccue.guava.concurrent;
import static dev.mccue.guava.collect.Lists.newArrayList;
import dev.mccue.guava.base.MoreObjects;
import dev.mccue.guava.base.Preconditions;
import dev.mccue.guava.base.Supplier;
import dev.mccue.guava.collect.ImmutableList;
import dev.mccue.guava.collect.MapMaker;
import dev.mccue.guava.math.IntMath;
import dev.mccue.guava.primitives.Ints;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A striped {@code Lock/Semaphore/ReadWriteLock}. This offers the underlying lock striping similar
* to that of {@code ConcurrentHashMap} in a reusable form, and extends it for semaphores and
* read-write locks. Conceptually, lock striping is the technique of dividing a lock into many
* stripes, increasing the granularity of a single lock and allowing independent operations
* to lock different stripes and proceed concurrently, instead of creating contention for a single
* lock.
*
*
The guarantee provided by this class is that equal keys lead to the same lock (or semaphore),
* i.e. {@code if (key1.equals(key2))} then {@code striped.get(key1) == striped.get(key2)} (assuming
* {@code Object#hashCode()} is correctly implemented for the keys). Note that if {@code key1} is
* not equal to {@code key2}, it is not guaranteed that {@code
* striped.get(key1) != striped.get(key2)}; the elements might nevertheless be mapped to the same
* lock. The lower the number of stripes, the higher the probability of this happening.
*
*
There are three flavors of this class: {@code Striped}, {@code Striped}, and
* {@code Striped}. For each type, two implementations are offered: {@code
* #lock(int) strong} and {@code #lazyWeakLock(int) weak} {@code Striped}, {@code
* #semaphore(int, int) strong} and {@code #lazyWeakSemaphore(int, int) weak} {@code
* Striped}, and {@code #readWriteLock(int) strong} and {@code
* #lazyWeakReadWriteLock(int) weak} {@code Striped}. Strong means that all
* stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless {@code Striped}
* itself is reclaimable. Weak means that locks/semaphores are created lazily, and they are
* allowed to be reclaimed if nobody is holding on to them. This is useful, for example, if one
* wants to create a {@code Striped} of many locks, but worries that in most cases only a
* small portion of these would be in use.
*
*
Prior to this class, one might be tempted to use {@code Map}, where {@code K}
* represents the task. This maximizes concurrency by having each unique key mapped to a unique
* lock, but also maximizes memory footprint. On the other extreme, one could use a single lock for
* all tasks, which minimizes memory footprint but also minimizes concurrency. Instead of choosing
* either of these extremes, {@code Striped} allows the user to trade between required concurrency
* and memory footprint. For example, if a set of tasks are CPU-bound, one could easily create a
* very compact {@code Striped} of {@code availableProcessors() * 4} stripes, instead of
* possibly thousands of locks which could be created in a {@code Map} structure.
*
* @author Dimitris Andreou
* @since 13.0
*/
@ElementTypesAreNonnullByDefault
public abstract class Striped {
/**
* If there are at least this many stripes, we assume the memory usage of a ConcurrentMap will be
* smaller than a large array. (This assumes that in the lazy case, most stripes are unused. As
* always, if many stripes are in use, a non-lazy striped makes more sense.)
*/
private static final int LARGE_LAZY_CUTOFF = 1024;
private Striped() {}
/**
* Returns the stripe that corresponds to the passed key. It is always guaranteed that if {@code
* key1.equals(key2)}, then {@code get(key1) == get(key2)}.
*
* @param key an arbitrary, non-null key
* @return the stripe that the passed key corresponds to
*/
public abstract L get(Object key);
/**
* Returns the stripe at the specified index. Valid indexes are 0, inclusively, to {@code size()},
* exclusively.
*
* @param index the index of the stripe to return; must be in {@code [0...size())}
* @return the stripe at the specified index
*/
public abstract L getAt(int index);
/**
* Returns the index to which the given key is mapped, so that getAt(indexFor(key)) == get(key).
*/
abstract int indexFor(Object key);
/** Returns the total number of stripes in this instance. */
public abstract int size();
/**
* Returns the stripes that correspond to the passed objects, in ascending (as per {@code
* #getAt(int)}) order. Thus, threads that use the stripes in the order returned by this method
* are guaranteed to not deadlock each other.
*
*
It should be noted that using a {@code Striped} with relatively few stripes, and {@code
* bulkGet(keys)} with a relative large number of keys can cause an excessive number of shared
* stripes (much like the birthday paradox, where much fewer than anticipated birthdays are needed
* for a pair of them to match). Please consider carefully the implications of the number of
* stripes, the intended concurrency level, and the typical number of keys used in a {@code
* bulkGet(keys)} operation. See Balls in
* Bins model for mathematical formulas that can be used to estimate the probability of
* collisions.
*
* @param keys arbitrary non-null keys
* @return the stripes corresponding to the objects (one per each object, derived by delegating to
* {@code #get(Object)}; may contain duplicates), in an increasing index order.
*/
public Iterable bulkGet(Iterable extends Object> keys) {
// Initially using the list to store the keys, then reusing it to store the respective L's
List