Skip to content

Commit f28ff77

Browse files
committed
add runInWriteLock with two args
1 parent c1fe466 commit f28ff77

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,31 @@ default void writeUnlock(long stamp) {
551551
return this;
552552
}
553553

554+
/**
555+
* Execute a function under {@link #writeLock()} block.
556+
*
557+
* @param first the first argument for the function.
558+
* @param second the second argument for the function.
559+
* @param function the function.
560+
* @param <A> the first argument's type.
561+
* @param <T> the second argument's type.
562+
* @return this array.
563+
* @since 9.9.0
564+
*/
565+
default <A, T> @NotNull ConcurrentArray<E> runInWriteLock(
566+
@NotNull A first,
567+
@NotNull T second,
568+
@NotNull NotNullTripleConsumer<ConcurrentArray<E>, A, T> function
569+
) {
570+
var stamp = writeLock();
571+
try {
572+
function.accept(this, first, second);
573+
} finally {
574+
writeUnlock(stamp);
575+
}
576+
return this;
577+
}
578+
554579
/**
555580
* Search an element using the condition under {@link #readLock()} block.
556581
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ void runInWriteLockTest() {
241241

242242
var array = ConcurrentArray.of("First", "Second", "Third", " ");
243243

244+
array.runInWriteLock(1, "Second", (arr, first, second) -> assertEquals(second, arr.get(first)));
245+
244246
array.runInWriteLock(object -> object.remove("Second"));
245247

246248
assertEquals(3, array.size());

0 commit comments

Comments
 (0)