diff --git a/android/guava-tests/test/com/google/common/util/concurrent/StripedTest.java b/android/guava-tests/test/com/google/common/util/concurrent/StripedTest.java index 688d6fd34ac0..ff826c32e32b 100644 --- a/android/guava-tests/test/com/google/common/util/concurrent/StripedTest.java +++ b/android/guava-tests/test/com/google/common/util/concurrent/StripedTest.java @@ -50,22 +50,8 @@ private static List> strongImplementations() { Striped.readWriteLock(256), Striped.lock(100), Striped.lock(256), - Striped.custom( - 100, - new Supplier() { - @Override - public Lock get() { - return new ReentrantLock(true); - } - }), - Striped.custom( - 256, - new Supplier() { - @Override - public Lock get() { - return new ReentrantLock(true); - } - }), + Striped.custom(100, FAIR_LOCK_SUPPLER), + Striped.custom(256, FAIR_LOCK_SUPPLER), Striped.semaphore(100, 1), Striped.semaphore(256, 1)); } @@ -86,6 +72,14 @@ public Lock get() { } }; + private static final Supplier FAIR_LOCK_SUPPLER = + new Supplier() { + @Override + public Lock get() { + return new ReentrantLock(true); + } + }; + private static final Supplier SEMAPHORE_SUPPLER = new Supplier() { @Override @@ -108,6 +102,8 @@ private static List> weakImplementations() { .add(new Striped.SmallLazyStriped(64, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped(50, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped(64, SEMAPHORE_SUPPLER)) + .add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER)) + .add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER)) .build(); } diff --git a/android/guava/src/com/google/common/util/concurrent/Striped.java b/android/guava/src/com/google/common/util/concurrent/Striped.java index 37fa2fd7ef97..74684cec8fa2 100644 --- a/android/guava/src/com/google/common/util/concurrent/Striped.java +++ b/android/guava/src/com/google/common/util/concurrent/Striped.java @@ -217,10 +217,18 @@ public static Striped lock(int stripes) { * @return a new {@code Striped} */ public static Striped lazyWeakLock(int stripes) { - return lazy(stripes, () -> new ReentrantLock(false)); + return lazyWeakCustom(stripes, () -> new ReentrantLock(false)); } - private static Striped lazy(int stripes, Supplier supplier) { + /** + * Creates a {@code Striped} with lazily initialized, weakly referenced locks. Every lock is + * obtained from the passed supplier. + * + * @param stripes the minimum number of stripes (locks) required + * @param supplier a {@code Supplier} object to obtain locks from + * @return a new {@code Striped} + */ + static Striped lazyWeakCustom(int stripes, Supplier supplier) { return stripes < LARGE_LAZY_CUTOFF ? new SmallLazyStriped(stripes, supplier) : new LargeLazyStriped(stripes, supplier); @@ -247,7 +255,7 @@ public static Striped semaphore(int stripes, int permits) { * @return a new {@code Striped} */ public static Striped lazyWeakSemaphore(int stripes, int permits) { - return lazy(stripes, () -> new Semaphore(permits, false)); + return lazyWeakCustom(stripes, () -> new Semaphore(permits, false)); } /** @@ -269,7 +277,7 @@ public static Striped readWriteLock(int stripes) { * @return a new {@code Striped} */ public static Striped lazyWeakReadWriteLock(int stripes) { - return lazy(stripes, WeakSafeReadWriteLock::new); + return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new); } /** * ReadWriteLock implementation whose read and write locks retain a reference back to this lock. diff --git a/guava-tests/test/com/google/common/util/concurrent/StripedTest.java b/guava-tests/test/com/google/common/util/concurrent/StripedTest.java index 688d6fd34ac0..ff826c32e32b 100644 --- a/guava-tests/test/com/google/common/util/concurrent/StripedTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/StripedTest.java @@ -50,22 +50,8 @@ private static List> strongImplementations() { Striped.readWriteLock(256), Striped.lock(100), Striped.lock(256), - Striped.custom( - 100, - new Supplier() { - @Override - public Lock get() { - return new ReentrantLock(true); - } - }), - Striped.custom( - 256, - new Supplier() { - @Override - public Lock get() { - return new ReentrantLock(true); - } - }), + Striped.custom(100, FAIR_LOCK_SUPPLER), + Striped.custom(256, FAIR_LOCK_SUPPLER), Striped.semaphore(100, 1), Striped.semaphore(256, 1)); } @@ -86,6 +72,14 @@ public Lock get() { } }; + private static final Supplier FAIR_LOCK_SUPPLER = + new Supplier() { + @Override + public Lock get() { + return new ReentrantLock(true); + } + }; + private static final Supplier SEMAPHORE_SUPPLER = new Supplier() { @Override @@ -108,6 +102,8 @@ private static List> weakImplementations() { .add(new Striped.SmallLazyStriped(64, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped(50, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped(64, SEMAPHORE_SUPPLER)) + .add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER)) + .add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER)) .build(); } diff --git a/guava/src/com/google/common/util/concurrent/Striped.java b/guava/src/com/google/common/util/concurrent/Striped.java index 37fa2fd7ef97..74684cec8fa2 100644 --- a/guava/src/com/google/common/util/concurrent/Striped.java +++ b/guava/src/com/google/common/util/concurrent/Striped.java @@ -217,10 +217,18 @@ public static Striped lock(int stripes) { * @return a new {@code Striped} */ public static Striped lazyWeakLock(int stripes) { - return lazy(stripes, () -> new ReentrantLock(false)); + return lazyWeakCustom(stripes, () -> new ReentrantLock(false)); } - private static Striped lazy(int stripes, Supplier supplier) { + /** + * Creates a {@code Striped} with lazily initialized, weakly referenced locks. Every lock is + * obtained from the passed supplier. + * + * @param stripes the minimum number of stripes (locks) required + * @param supplier a {@code Supplier} object to obtain locks from + * @return a new {@code Striped} + */ + static Striped lazyWeakCustom(int stripes, Supplier supplier) { return stripes < LARGE_LAZY_CUTOFF ? new SmallLazyStriped(stripes, supplier) : new LargeLazyStriped(stripes, supplier); @@ -247,7 +255,7 @@ public static Striped semaphore(int stripes, int permits) { * @return a new {@code Striped} */ public static Striped lazyWeakSemaphore(int stripes, int permits) { - return lazy(stripes, () -> new Semaphore(permits, false)); + return lazyWeakCustom(stripes, () -> new Semaphore(permits, false)); } /** @@ -269,7 +277,7 @@ public static Striped readWriteLock(int stripes) { * @return a new {@code Striped} */ public static Striped lazyWeakReadWriteLock(int stripes) { - return lazy(stripes, WeakSafeReadWriteLock::new); + return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new); } /** * ReadWriteLock implementation whose read and write locks retain a reference back to this lock.