Skip to content

Commit 7e996d1

Browse files
committed
add runInReadLock, getInWriteLock and getInReadLock with two args
1 parent 64af36b commit 7e996d1

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010

11-
rootProject.version = '9.9.0'
11+
rootProject.version = '9.10.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

rlib-common/src/main/java/com/ss/rlib/common/util/array/ConcurrentArray.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,31 @@ default void writeUnlock(long stamp) {
353353
}
354354
}
355355

356+
/**
357+
* Execute a function to get some result under {@link #readLock()} block.
358+
*
359+
* @param first the first argument for the function.
360+
* @param second the second argument for the function.
361+
* @param function the function.
362+
* @param <A> the first argument's type.
363+
* @param <T> the second argument's type.
364+
* @param <R> the result's type.
365+
* @return the result from the function.
366+
* @since 9.10.0
367+
*/
368+
default <A, T, R> @Nullable R getInReadLock(
369+
@NotNull A first,
370+
@NotNull T second,
371+
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, A, T, R> function
372+
) {
373+
var stamp = readLock();
374+
try {
375+
return function.apply(this, first, second);
376+
} finally {
377+
readUnlock(stamp);
378+
}
379+
}
380+
356381
/**
357382
* Execute a function to get some result under {@link #writeLock()} block.
358383
*
@@ -391,6 +416,31 @@ default void writeUnlock(long stamp) {
391416
}
392417
}
393418

419+
/**
420+
* Execute a function to get some result under {@link #writeLock()} block.
421+
*
422+
* @param first the first argument for the function.
423+
* @param second the second argument for the function.
424+
* @param function the function.
425+
* @param <A> the first argument's type.
426+
* @param <T> the second argument's type.
427+
* @param <R> the result's type.
428+
* @return the result from the function.
429+
* @since 9.10.0
430+
*/
431+
default <A, T, R> @Nullable R getInWriteLock(
432+
@NotNull A first,
433+
@NotNull T second,
434+
@NotNull NotNullNullableTripleFunction<ConcurrentArray<E>, A, T, R> function
435+
) {
436+
var stamp = writeLock();
437+
try {
438+
return function.apply(this, first, second);
439+
} finally {
440+
writeUnlock(stamp);
441+
}
442+
}
443+
394444
/**
395445
* Execute a function under {@link #readLock()} block.
396446
*
@@ -433,6 +483,33 @@ default void writeUnlock(long stamp) {
433483
return this;
434484
}
435485

486+
/**
487+
* Execute a function under {@link #readLock()} block.
488+
*
489+
* @param <A> the first argument's type.
490+
* @param <T> the second argument's type.
491+
* @param first the first argument.
492+
* @param second the second argument.
493+
* @param function the function.
494+
* @return this array.
495+
* @since 9.10.0
496+
*/
497+
default <A, T> @NotNull ConcurrentArray<E> runInReadLock(
498+
@NotNull A first,
499+
@NotNull T second,
500+
@NotNull NotNullTripleConsumer<ConcurrentArray<E>, A, T> function
501+
) {
502+
503+
var stamp = readLock();
504+
try {
505+
function.accept(this, first, second);
506+
} finally {
507+
readUnlock(stamp);
508+
}
509+
510+
return this;
511+
}
512+
436513
/**
437514
* Execute a function under {@link #writeLock()} block.
438515
*

rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ConcurrentArrayTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ void runInReadLockTest() {
270270

271271
array.runInReadLock("Third", (arr, arg) ->
272272
assertEquals(2, arr.count(arg, String::equals)));
273+
274+
array.runInReadLock(2, "Third", (arr, first, second) -> {
275+
assertType(arr, Array.class);
276+
assertIntType(first);
277+
assertType(second, String.class);
278+
assertEquals(first, arr.count(second, String::equals));
279+
});
273280
}
274281

275282
@Test
@@ -279,6 +286,10 @@ void getInWriteLockTest() {
279286

280287
assertEquals("Third", array.getInWriteLock(arr -> arr.get(2)));
281288
assertEquals("Second", array.getInWriteLock(1, Array::get));
289+
assertEquals(
290+
"FirstSecond",
291+
array.getInWriteLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second))
292+
);
282293
}
283294

284295
@Test
@@ -288,6 +299,10 @@ void getInReadLockTest() {
288299

289300
assertEquals("Third", array.getInReadLock(arr -> arr.get(2)));
290301
assertEquals("Second", array.getInReadLock(1, Array::get));
302+
assertEquals(
303+
"FirstSecond",
304+
array.getInReadLock(0, 1, (arr, first, second) -> arr.get(first) + arr.get(second))
305+
);
291306
}
292307

293308
@Test

0 commit comments

Comments
 (0)