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.
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
/*
* 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 com.google.common.util.concurrent;
import static com.google.common.collect.Lists.newArrayList;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.MapMaker;
import com.google.common.math.IntMath;
import com.google.common.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
* {@link 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: {@linkplain
* #lock(int) strong} and {@linkplain #lazyWeakLock(int) weak} {@code Striped}, {@linkplain
* #semaphore(int, int) strong} and {@linkplain #lazyWeakSemaphore(int, int) weak} {@code
* Striped}, and {@linkplain #readWriteLock(int) strong} and {@linkplain
* #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
*/
@Beta
@GwtIncompatible
@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 {@link
* #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
* {@link #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