Skip to content

Commit 4b96e43

Browse files
java-team-github-botGoogle Java Core Libraries
authored andcommitted
Internal change.
RELNOTES=n/a PiperOrigin-RevId: 646915665
1 parent b50df33 commit 4b96e43

File tree

4 files changed

+48
-40
lines changed

4 files changed

+48
-40
lines changed

android/guava-tests/test/com/google/common/util/concurrent/StripedTest.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,8 @@ private static List<Striped<?>> strongImplementations() {
5050
Striped.readWriteLock(256),
5151
Striped.lock(100),
5252
Striped.lock(256),
53-
Striped.custom(
54-
100,
55-
new Supplier<Lock>() {
56-
@Override
57-
public Lock get() {
58-
return new ReentrantLock(true);
59-
}
60-
}),
61-
Striped.custom(
62-
256,
63-
new Supplier<Lock>() {
64-
@Override
65-
public Lock get() {
66-
return new ReentrantLock(true);
67-
}
68-
}),
53+
Striped.custom(100, FAIR_LOCK_SUPPLER),
54+
Striped.custom(256, FAIR_LOCK_SUPPLER),
6955
Striped.semaphore(100, 1),
7056
Striped.semaphore(256, 1));
7157
}
@@ -86,6 +72,14 @@ public Lock get() {
8672
}
8773
};
8874

75+
private static final Supplier<Lock> FAIR_LOCK_SUPPLER =
76+
new Supplier<Lock>() {
77+
@Override
78+
public Lock get() {
79+
return new ReentrantLock(true);
80+
}
81+
};
82+
8983
private static final Supplier<Semaphore> SEMAPHORE_SUPPLER =
9084
new Supplier<Semaphore>() {
9185
@Override
@@ -108,6 +102,8 @@ private static List<Striped<?>> weakImplementations() {
108102
.add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
109103
.add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
110104
.add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
105+
.add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER))
106+
.add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER))
111107
.build();
112108
}
113109

android/guava/src/com/google/common/util/concurrent/Striped.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,18 @@ public static Striped<Lock> lock(int stripes) {
217217
* @return a new {@code Striped<Lock>}
218218
*/
219219
public static Striped<Lock> lazyWeakLock(int stripes) {
220-
return lazy(stripes, () -> new ReentrantLock(false));
220+
return lazyWeakCustom(stripes, () -> new ReentrantLock(false));
221221
}
222222

223-
private static <L> Striped<L> lazy(int stripes, Supplier<L> supplier) {
223+
/**
224+
* Creates a {@code Striped<L>} with lazily initialized, weakly referenced locks. Every lock is
225+
* obtained from the passed supplier.
226+
*
227+
* @param stripes the minimum number of stripes (locks) required
228+
* @param supplier a {@code Supplier<L>} object to obtain locks from
229+
* @return a new {@code Striped<L>}
230+
*/
231+
static <L> Striped<L> lazyWeakCustom(int stripes, Supplier<L> supplier) {
224232
return stripes < LARGE_LAZY_CUTOFF
225233
? new SmallLazyStriped<L>(stripes, supplier)
226234
: new LargeLazyStriped<L>(stripes, supplier);
@@ -247,7 +255,7 @@ public static Striped<Semaphore> semaphore(int stripes, int permits) {
247255
* @return a new {@code Striped<Semaphore>}
248256
*/
249257
public static Striped<Semaphore> lazyWeakSemaphore(int stripes, int permits) {
250-
return lazy(stripes, () -> new Semaphore(permits, false));
258+
return lazyWeakCustom(stripes, () -> new Semaphore(permits, false));
251259
}
252260

253261
/**
@@ -269,7 +277,7 @@ public static Striped<ReadWriteLock> readWriteLock(int stripes) {
269277
* @return a new {@code Striped<ReadWriteLock>}
270278
*/
271279
public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
272-
return lazy(stripes, WeakSafeReadWriteLock::new);
280+
return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new);
273281
}
274282
/**
275283
* ReadWriteLock implementation whose read and write locks retain a reference back to this lock.

guava-tests/test/com/google/common/util/concurrent/StripedTest.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,8 @@ private static List<Striped<?>> strongImplementations() {
5050
Striped.readWriteLock(256),
5151
Striped.lock(100),
5252
Striped.lock(256),
53-
Striped.custom(
54-
100,
55-
new Supplier<Lock>() {
56-
@Override
57-
public Lock get() {
58-
return new ReentrantLock(true);
59-
}
60-
}),
61-
Striped.custom(
62-
256,
63-
new Supplier<Lock>() {
64-
@Override
65-
public Lock get() {
66-
return new ReentrantLock(true);
67-
}
68-
}),
53+
Striped.custom(100, FAIR_LOCK_SUPPLER),
54+
Striped.custom(256, FAIR_LOCK_SUPPLER),
6955
Striped.semaphore(100, 1),
7056
Striped.semaphore(256, 1));
7157
}
@@ -86,6 +72,14 @@ public Lock get() {
8672
}
8773
};
8874

75+
private static final Supplier<Lock> FAIR_LOCK_SUPPLER =
76+
new Supplier<Lock>() {
77+
@Override
78+
public Lock get() {
79+
return new ReentrantLock(true);
80+
}
81+
};
82+
8983
private static final Supplier<Semaphore> SEMAPHORE_SUPPLER =
9084
new Supplier<Semaphore>() {
9185
@Override
@@ -108,6 +102,8 @@ private static List<Striped<?>> weakImplementations() {
108102
.add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
109103
.add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER))
110104
.add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER))
105+
.add(Striped.lazyWeakCustom(50, FAIR_LOCK_SUPPLER))
106+
.add(Striped.lazyWeakCustom(64, FAIR_LOCK_SUPPLER))
111107
.build();
112108
}
113109

guava/src/com/google/common/util/concurrent/Striped.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,18 @@ public static Striped<Lock> lock(int stripes) {
217217
* @return a new {@code Striped<Lock>}
218218
*/
219219
public static Striped<Lock> lazyWeakLock(int stripes) {
220-
return lazy(stripes, () -> new ReentrantLock(false));
220+
return lazyWeakCustom(stripes, () -> new ReentrantLock(false));
221221
}
222222

223-
private static <L> Striped<L> lazy(int stripes, Supplier<L> supplier) {
223+
/**
224+
* Creates a {@code Striped<L>} with lazily initialized, weakly referenced locks. Every lock is
225+
* obtained from the passed supplier.
226+
*
227+
* @param stripes the minimum number of stripes (locks) required
228+
* @param supplier a {@code Supplier<L>} object to obtain locks from
229+
* @return a new {@code Striped<L>}
230+
*/
231+
static <L> Striped<L> lazyWeakCustom(int stripes, Supplier<L> supplier) {
224232
return stripes < LARGE_LAZY_CUTOFF
225233
? new SmallLazyStriped<L>(stripes, supplier)
226234
: new LargeLazyStriped<L>(stripes, supplier);
@@ -247,7 +255,7 @@ public static Striped<Semaphore> semaphore(int stripes, int permits) {
247255
* @return a new {@code Striped<Semaphore>}
248256
*/
249257
public static Striped<Semaphore> lazyWeakSemaphore(int stripes, int permits) {
250-
return lazy(stripes, () -> new Semaphore(permits, false));
258+
return lazyWeakCustom(stripes, () -> new Semaphore(permits, false));
251259
}
252260

253261
/**
@@ -269,7 +277,7 @@ public static Striped<ReadWriteLock> readWriteLock(int stripes) {
269277
* @return a new {@code Striped<ReadWriteLock>}
270278
*/
271279
public static Striped<ReadWriteLock> lazyWeakReadWriteLock(int stripes) {
272-
return lazy(stripes, WeakSafeReadWriteLock::new);
280+
return lazyWeakCustom(stripes, WeakSafeReadWriteLock::new);
273281
}
274282
/**
275283
* ReadWriteLock implementation whose read and write locks retain a reference back to this lock.

0 commit comments

Comments
 (0)