diff --git a/README.md b/README.md index 6b083dc3c13251d6f241add2131731d95a0ec15d..4f957a7ad8c8dc2fb85b29ddb133e58c7da8a60f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ +

Icon +

# PDConcurrent diff --git a/build.gradle b/build.gradle index 7b260779b34aa9e5c95eed842ffaddfb4b20705d..0a57b757924e7f67710424dc36c2554a632c6b5b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ tasks.withType(JavaCompile).configureEach { } group 'fybug.nulll' -version = '0.1.0' +version = '0.1.3' java { toolchain { @@ -53,12 +53,13 @@ dependencies { // lombok testCompileOnly "org.projectlombok:lombok:+" testAnnotationProcessor "org.projectlombok:lombok:+" - testRuntimeOnly "org.junit.jupiter:junit-jupiter:5.12.0+" + testImplementation platform('org.junit:junit-bom:5.10.0') + testRuntimeOnly "org.junit.jupiter:junit-jupiter:5.12.0-RC2" } test { dependencies { - testImplementation "org.junit.jupiter:junit-jupiter:5.12.0+" + testImplementation "org.junit.jupiter:junit-jupiter:5.12.0-RC2" } useJUnitPlatform() } diff --git a/jar/PDConcurrent_bin.jar b/jar/PDConcurrent_bin.jar index 7c201907b4d9571e7718389e537db8c357499f93..110ed946a8e69be780d0c311d65e7ae7948dd92d 100644 Binary files a/jar/PDConcurrent_bin.jar and b/jar/PDConcurrent_bin.jar differ diff --git a/jar/PDConcurrent_sources.jar b/jar/PDConcurrent_sources.jar index e3154d1e5af5524fde7b237cd5877ad67a2b35db..e75492de20527190730270db4095627637c8c2a4 100644 Binary files a/jar/PDConcurrent_sources.jar and b/jar/PDConcurrent_sources.jar differ diff --git a/src/main/java/fybug/nulll/pdconcurrent/SyLock.java b/src/main/java/fybug/nulll/pdconcurrent/SyLock.java index 32216c4a5694b1b97644d6b13d836884c9aa3d05..e21646be6aa8339513f1de0dc44d96cc6ad2ca78 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/SyLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/SyLock.java @@ -1,7 +1,9 @@ package fybug.nulll.pdconcurrent; import fybug.nulll.pdconcurrent.e.LockType; -import fybug.nulll.pdconcurrent.i.ReadWriteLock; -import fybug.nulll.pdconcurrent.i.TryReadWriteLock; +import fybug.nulll.pdconcurrent.i.simple.ReadLockSimple; +import fybug.nulll.pdconcurrent.i.simple.TryReadLockSimple; +import fybug.nulll.pdconcurrent.i.simple.TryWriteLockSimple; +import fybug.nulll.pdconcurrent.i.simple.WriteLockSimple; import fybug.nulll.pdconcurrent.lock.ObjLock; import fybug.nulll.pdconcurrent.lock.RWLock; import fybug.nulll.pdconcurrent.lock.ReLock; @@ -9,7 +11,7 @@ import jakarta.validation.constraints.NotNull; /** *

并发管理.

- * 通过传入回调的方式隐藏内部的并发管理方法,并支持复用内部的try块,通过传入的回调插入到catch,finally块中执行
+ * 通过传入回调的方式隐藏内部的并发管理方法,并支持复用内部的 try 块,通过传入的回调插入到 catch,finally 块中执行
* {@code lock()}方法用于根据传入的{@link LockType}申请不同的锁类型进行执行
* {@code read()}方法用于使用读锁执行,{@code write()}用于使用写锁执行,只有在使用读写锁实现{@link RWLock}才有区别。其余实现两个之间无区别
* {@code trylock()}类型的方法为尝试获取锁的实现,这意味着并不是一定能获取到锁,但也意味着不需要等待
@@ -18,11 +20,12 @@ import jakarta.validation.constraints.NotNull; * 使用 {@code new**Lock()} 的方法获取不同并发管理的实例
* * @author fybug - * @version 0.1.2 + * @version 0.1.3 * @since PDConcurrent 0.0.1 */ +@SuppressWarnings("unused") public -interface SyLock extends ReadWriteLock, TryReadWriteLock { +interface SyLock extends ReadLockSimple, WriteLockSimple, TryReadLockSimple, TryWriteLockSimple { /** * 获取传统并发实现 * @@ -30,7 +33,6 @@ interface SyLock extends ReadWriteLock, TryReadWriteLock { * * @see ObjLock */ - @SuppressWarnings("unused") @NotNull static ObjLock newObjLock() { return new ObjLock(); } @@ -42,7 +44,6 @@ interface SyLock extends ReadWriteLock, TryReadWriteLock { * * @see ReLock */ - @SuppressWarnings("unused") @NotNull static ReLock newReLock() { return new ReLock(); } @@ -54,7 +55,6 @@ interface SyLock extends ReadWriteLock, TryReadWriteLock { * * @see RWLock */ - @SuppressWarnings("unused") @NotNull static RWLock newRWLock() { return new RWLock(); } diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/Lock.java b/src/main/java/fybug/nulll/pdconcurrent/i/Lock.java index 7c5a94f5d5f53a2b808cb076309ba50bdb92ba8f..de52725a072334de92ced7e77c6df3fa28550abc 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/i/Lock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/i/Lock.java @@ -10,15 +10,16 @@ import jakarta.validation.constraints.NotNull; /** *

基础锁管理.

- * 通过传入回调的方式隐藏内部的并发管理方法,并支持复用内部的try块,通过传入的回调插入到catch,finally块中执行
- * {@code lock()}方法用于根据传入的{@link LockType}申请不同的锁类型进行执行
+ * 通过传入回调的方式隐藏内部的并发管理方法,并支持复用内部的 try 块,通过传入的回调插入到 catch,finally 块中执行
+ * {@code lock()} 方法用于根据传入的 {@link LockType} 申请不同的锁类型进行执行
* 有可抛出异常的方法变体,可在传入的接口中抛出异常 * * @author fybug - * @version 0.0.1 + * @version 0.0.2 * @see LockType * @since i 0.0.1 */ +public interface Lock { /** * 使用锁执行指定回调 @@ -44,11 +45,6 @@ interface Lock { * 使用锁执行指定回调 *

* {@link #lock(LockType, trySupplier, Function, Function)}的无返回变体 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 */ default void lock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Consumer catchby, @@ -85,33 +81,10 @@ interface Lock { */ R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception; - /** - * 使用锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier, Function)}的简易变体 - * - * @param lockType 锁类型 - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R lock(@NotNull LockType lockType, @NotNull trySupplier run) throws Exception - { return lock(lockType, run, null); } - /** * 使用锁执行指定回调 *

* {@link #lock(LockType, trySupplier, Function)}的无返回变体 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 */ default void lock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception { @@ -123,18 +96,4 @@ interface Lock { return null; }); } - - /** - * 使用锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable, Runnable)}的简易变体 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void lock(@NotNull LockType lockType, @NotNull tryRunnable run) throws Exception - { lock(lockType, run, null); } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/ReadLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/ReadLock.java new file mode 100644 index 0000000000000000000000000000000000000000..1824694a640efd5a1b72e203c6109089eabd0b9f --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/ReadLock.java @@ -0,0 +1,60 @@ +package fybug.nulll.pdconcurrent.i; +import java.util.function.Consumer; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryRunnable; +import fybug.nulll.pdconcurrent.fun.trySupplier; +import fybug.nulll.pdconcurrent.i.simple.LockSimple; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; + +/** + *

并发管理读锁支持拓展.

+ * {@link Lock}的拓展,增加 {@code read()} 类方法用于隐藏 {@link LockType} 参数 + * + * @author fybug + * @version 0.0.1 + * @see LockType#READ + * @see Lock + * @since i 0.0.2 + */ +@SuppressWarnings("unused") +public +interface ReadLock extends LockSimple { + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier, Function, Function)}指定读锁的变种 + */ + default + R read(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) + { return lock(LockType.READ, run, catchby, finaby); } + + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable, Consumer, Runnable)}指定读锁的变种 + */ + default + void read(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { lock(LockType.READ, run, catchby, finaby); } + + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier, Function)}指定读锁的变种 + */ + default + R read(@NotNull trySupplier run, @Nullable Function finaby) throws Exception + { return lock(LockType.READ, run, finaby); } + + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable, Runnable)}指定读锁的变种 + */ + default + void read(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception + { lock(LockType.READ, run, finaby); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/ReadWriteLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/ReadWriteLock.java deleted file mode 100644 index 63a7d1ea5ad2e3546443f33dd218180566acd366..0000000000000000000000000000000000000000 --- a/src/main/java/fybug/nulll/pdconcurrent/i/ReadWriteLock.java +++ /dev/null @@ -1,203 +0,0 @@ -package fybug.nulll.pdconcurrent.i; -import java.util.function.Consumer; -import java.util.function.Function; - -import fybug.nulll.pdconcurrent.e.LockType; -import fybug.nulll.pdconcurrent.fun.tryRunnable; -import fybug.nulll.pdconcurrent.fun.trySupplier; -import jakarta.annotation.Nullable; -import jakarta.validation.constraints.NotNull; - -/** - *

并发管理读写支持拓展.

- * {@link Lock}的拓展,增加{@code read()}、{@code write()}类方法用于隐藏{@link LockType}参数 - * - * @author fybug - * @version 0.0.1 - * @see LockType#READ - * @see LockType#WRITE - * @see Lock - * @since i 0.0.1 - */ -@SuppressWarnings("unused") -public -interface ReadWriteLock extends Lock { - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier, Function, Function)}指定读锁的变种 - * - * @param run 带返回的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - */ - default - R read(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) - { return lock(LockType.READ, run, catchby, finaby); } - - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable, Consumer, Runnable)}指定读锁的变种 - * - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - */ - default - void read(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) - { lock(LockType.READ, run, catchby, finaby); } - - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier, Function)}指定读锁的变种 - * - * @param run 带返回的回调 - * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R read(@NotNull trySupplier run, @Nullable Function finaby) throws Exception - { return lock(LockType.READ, run, finaby); } - - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier)}指定读锁的变种 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R read(@NotNull trySupplier run) throws Exception - { return lock(LockType.READ, run); } - - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable, Runnable)}指定读锁的变种 - * - * @param run 执行的回调 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void read(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception - { lock(LockType.READ, run, finaby); } - - /** - * 使用读锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable)}指定读锁的变种 - * - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void read(@NotNull tryRunnable run) throws Exception - { lock(LockType.READ, run); } - - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier, Function, Function)}指定写锁的变种 - * - * @param run 带返回的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - */ - default - R write(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) - { return lock(LockType.WRITE, run, catchby, finaby); } - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable, Consumer, Runnable)}指定写锁的变种 - * - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - */ - default - void write(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) - { lock(LockType.WRITE, run, catchby, finaby); } - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier, Function)}指定写锁的变种 - * - * @param run 带返回的回调 - * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R write(@NotNull trySupplier run, @Nullable Function finaby) throws Exception - { return lock(LockType.WRITE, run, finaby); } - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, trySupplier)}指定写锁的变种 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R write(@NotNull trySupplier run) throws Exception - { return lock(LockType.WRITE, run); } - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable, Runnable)}指定写锁的变种 - * - * @param run 执行的回调 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void write(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception - { lock(LockType.WRITE, run, finaby); } - - /** - * 使用写锁执行指定回调 - *

- * {@link #lock(LockType, tryRunnable)}指定写锁的变种 - * - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void write(@NotNull tryRunnable run) throws Exception - { lock(LockType.WRITE, run); } -} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/TryLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/TryLock.java index b6d120fe914bdeb39884f50aa0b64e8ebb76545e..73c60439b441914f200db03fcd56c62d1d341a51 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/i/TryLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/i/TryLock.java @@ -14,9 +14,10 @@ import jakarta.validation.constraints.NotNull; * {@link Lock}的拓展,增加{@code trylock()}类方法用于适配需要立刻判断是否获取到锁并即刻往下执行的情况 * * @author fybug - * @version 0.0.1 + * @version 0.0.2 * @since i 0.0.1 */ +public interface TryLock { /** * 尝试使用锁执行指定回调 @@ -44,11 +45,6 @@ interface TryLock { * 尝试使用锁执行指定回调 *

* {@link #trylock(LockType, tryFunction, Function, Function)}的无返回变体 - * - * @param lockType 锁类型 - * @param run 执行的回调,传入参数是否获取到锁 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 */ default void trylock(@NotNull LockType lockType, @NotNull tryConsumer run, @Nullable Consumer catchby, @@ -87,33 +83,10 @@ interface TryLock { R trylock(@NotNull LockType lockType, @NotNull tryFunction run, @Nullable Function finaby) throws Exception; - /** - * 尝试使用锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction, Function)}的简易变体 - * - * @param lockType 锁类型 - * @param run 带返回的回调,传入参数是否获取到锁 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R trylock(@NotNull LockType lockType, @NotNull tryFunction run) throws Exception - { return trylock(lockType, run, null); } - /** * 尝试使用锁执行指定回调 *

* {@link #trylock(LockType, tryFunction, Function)}的无返回变体 - * - * @param lockType 锁类型 - * @param run 执行的回调,传入参数是否获取到锁 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 */ default void trylock(@NotNull LockType lockType, @NotNull tryConsumer run, @Nullable Runnable finaby) throws Exception { @@ -125,18 +98,4 @@ interface TryLock { return null; }); } - - /** - * 尝试使用锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer, Runnable)}的简易变体 - * - * @param lockType 锁类型 - * @param run 带返回的回调,传入参数是否获取到锁 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void trylock(@NotNull LockType lockType, @NotNull tryConsumer run) throws Exception - { trylock(lockType, run, null); } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/TryReadLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/TryReadLock.java new file mode 100644 index 0000000000000000000000000000000000000000..2f8b22dc016ecdfc6cbb4a6fc7789a1c579cbfe0 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/TryReadLock.java @@ -0,0 +1,61 @@ +package fybug.nulll.pdconcurrent.i; +import java.util.function.Consumer; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryConsumer; +import fybug.nulll.pdconcurrent.fun.tryFunction; +import fybug.nulll.pdconcurrent.i.simple.TryLockSimple; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; + +/** + *

TryLock 读锁支持拓展.

+ * {@link TryLock}的拓展,增加 {@code tryread()} 类方法用于隐藏 {@link LockType} 参数 + * + * @author fybug + * @version 0.0.1 + * @see LockType#READ + * @see TryLock + * @since i 0.0.2 + */ +@SuppressWarnings("unused") +public +interface TryReadLock extends TryLockSimple { + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction, Function, Function)}指定读锁的变种 + */ + default + R tryread(@NotNull tryFunction run, @Nullable Function catchby, + @Nullable Function finaby) + { return trylock(LockType.READ, run, catchby, finaby); } + + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer, Consumer, Runnable)}指定读锁的变种 + */ + default + void tryread(@NotNull tryConsumer run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { trylock(LockType.READ, run, catchby, finaby); } + + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction, Function)}指定读锁的变种 + */ + default + R tryread(@NotNull tryFunction run, @Nullable Function finaby) throws Exception + { return trylock(LockType.READ, run, finaby); } + + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer, Runnable)}指定读锁的变种 + */ + default + void tryread(@NotNull tryConsumer run, @Nullable Runnable finaby) throws Exception + { trylock(LockType.READ, run, finaby); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/TryReadWriteLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/TryReadWriteLock.java deleted file mode 100644 index a3ff383407714c5af311f74bcc25c79a332805ea..0000000000000000000000000000000000000000 --- a/src/main/java/fybug/nulll/pdconcurrent/i/TryReadWriteLock.java +++ /dev/null @@ -1,204 +0,0 @@ -package fybug.nulll.pdconcurrent.i; -import java.util.function.Consumer; -import java.util.function.Function; - -import fybug.nulll.pdconcurrent.e.LockType; -import fybug.nulll.pdconcurrent.fun.tryConsumer; -import fybug.nulll.pdconcurrent.fun.tryFunction; -import jakarta.annotation.Nullable; -import jakarta.validation.constraints.NotNull; - -/** - *

TryLock 读写支持拓展.

- * {@link TryLock}的拓展,增加{@code tryread()}、{@code trywrite()}类方法用于隐藏{@link LockType}参数 - * - * @author fybug - * @version 0.0.1 - * @see LockType#READ - * @see LockType#WRITE - * @see TryLock - * @since i 0.0.1 - */ -@SuppressWarnings("unused") -public -interface TryReadWriteLock extends TryLock { - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction, Function, Function)}指定读锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - */ - default - R tryread(@NotNull tryFunction run, @Nullable Function catchby, - @Nullable Function finaby) - { return trylock(LockType.READ, run, catchby, finaby); } - - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer, Consumer, Runnable)}指定读锁的变种 - * - * @param run 执行的回调,传入参数是否获取到锁 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - */ - default - void tryread(@NotNull tryConsumer run, @Nullable Consumer catchby, @Nullable Runnable finaby) - { trylock(LockType.READ, run, catchby, finaby); } - - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction, Function)}指定读锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值,遇到异常传入{@code null} - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R tryread(@NotNull tryFunction run, @Nullable Function finaby) throws Exception - { return trylock(LockType.READ, run, finaby); } - - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction)}指定读锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R tryread(@NotNull tryFunction run) throws Exception - { return trylock(LockType.READ, run); } - - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer, Runnable)}指定读锁的变种 - * - * @param run 执行的回调,传入参数是否获取到锁 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void tryread(@NotNull tryConsumer run, @Nullable Runnable finaby) throws Exception - { trylock(LockType.READ, run, finaby); } - - /** - * 尝试使用读锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer)}指定读锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void tryread(@NotNull tryConsumer run) throws Exception - { trylock(LockType.READ, run); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction, Function, Function)}指定写锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - */ - default - R trywrite(@NotNull tryFunction run, @Nullable Function catchby, - @Nullable Function finaby) - { return trylock(LockType.WRITE, run, catchby, finaby); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer, Consumer, Runnable)}指定写锁的变种 - * - * @param run 执行的回调,传入参数是否获取到锁 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - */ - default - void trywrite(@NotNull tryConsumer run, @Nullable Consumer catchby, @Nullable Runnable finaby) - { trylock(LockType.WRITE, run, catchby, finaby); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction, Function)}指定写锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值,遇到异常传入{@code null} - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R trywrite(@NotNull tryFunction run, @Nullable Function finaby) throws Exception - { return trylock(LockType.WRITE, run, finaby); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryFunction)}指定写锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - R trywrite(@NotNull tryFunction run) throws Exception - { return trylock(LockType.WRITE, run); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer, Runnable)}指定写锁的变种 - * - * @param run 执行的回调,传入参数是否获取到锁 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void trywrite(@NotNull tryConsumer run, @Nullable Runnable finaby) throws Exception - { trylock(LockType.WRITE, run, finaby); } - - /** - * 尝试使用写锁执行指定回调 - *

- * {@link #trylock(LockType, tryConsumer)}指定写锁的变种 - * - * @param run 带返回的回调,传入参数是否获取到锁 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - */ - default - void trywrite(@NotNull tryConsumer run) throws Exception - { trylock(LockType.WRITE, run); } -} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/TryWriteLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/TryWriteLock.java new file mode 100644 index 0000000000000000000000000000000000000000..75f55b7900ae4794972e589240b582618bea8516 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/TryWriteLock.java @@ -0,0 +1,61 @@ +package fybug.nulll.pdconcurrent.i; +import java.util.function.Consumer; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryConsumer; +import fybug.nulll.pdconcurrent.fun.tryFunction; +import fybug.nulll.pdconcurrent.i.simple.TryLockSimple; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; + +/** + *

TryLock 写锁支持拓展.

+ * {@link TryLock}的拓展,增加 {@code trywrite()} 类方法用于隐藏 {@link LockType} 参数 + * + * @author fybug + * @version 0.0.1 + * @see LockType#WRITE + * @see TryLock + * @since i 0.0.2 + */ +@SuppressWarnings("unused") +public +interface TryWriteLock extends TryLockSimple { + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction, Function, Function)}指定写锁的变种 + */ + default + R trywrite(@NotNull tryFunction run, @Nullable Function catchby, + @Nullable Function finaby) + { return trylock(LockType.WRITE, run, catchby, finaby); } + + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer, Consumer, Runnable)}指定写锁的变种 + */ + default + void trywrite(@NotNull tryConsumer run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { trylock(LockType.WRITE, run, catchby, finaby); } + + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction, Function)}指定写锁的变种 + */ + default + R trywrite(@NotNull tryFunction run, @Nullable Function finaby) throws Exception + { return trylock(LockType.WRITE, run, finaby); } + + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer, Runnable)}指定写锁的变种 + */ + default + void trywrite(@NotNull tryConsumer run, @Nullable Runnable finaby) throws Exception + { trylock(LockType.WRITE, run, finaby); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/WriteLock.java b/src/main/java/fybug/nulll/pdconcurrent/i/WriteLock.java new file mode 100644 index 0000000000000000000000000000000000000000..5b6f57479f27a0b2a887e632ddfd94c92d8893e4 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/WriteLock.java @@ -0,0 +1,60 @@ +package fybug.nulll.pdconcurrent.i; +import java.util.function.Consumer; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryRunnable; +import fybug.nulll.pdconcurrent.fun.trySupplier; +import fybug.nulll.pdconcurrent.i.simple.LockSimple; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; + +/** + *

并发管理写锁支持拓展.

+ * {@link Lock}的拓展,增加 {@code write()} 类方法用于隐藏 {@link LockType} 参数 + * + * @author fybug + * @version 0.0.1 + * @see LockType#WRITE + * @see Lock + * @since i 0.0.2 + */ +@SuppressWarnings("unused") +public +interface WriteLock extends LockSimple { + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier, Function, Function)}指定写锁的变种 + */ + default + R write(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) + { return lock(LockType.WRITE, run, catchby, finaby); } + + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable, Consumer, Runnable)}指定写锁的变种 + */ + default + void write(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { lock(LockType.WRITE, run, catchby, finaby); } + + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier, Function)}指定写锁的变种 + */ + default + R write(@NotNull trySupplier run, @Nullable Function finaby) throws Exception + { return lock(LockType.WRITE, run, finaby); } + + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable, Runnable)}指定写锁的变种 + */ + default + void write(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception + { lock(LockType.WRITE, run, finaby); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/package-info.java b/src/main/java/fybug/nulll/pdconcurrent/i/package-info.java index 6ced81b027b2698770bcba5df855e639b3fa8731..04495588a4792675defc03937e02c2f636fa6f77 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/i/package-info.java +++ b/src/main/java/fybug/nulll/pdconcurrent/i/package-info.java @@ -3,7 +3,7 @@ * 提供基础功能接口与部分功能框架实现 * * @author fybug - * @version 0.0.1 + * @version 0.0.2 * @since PDConcurrent 0.1.2 */ package fybug.nulll.pdconcurrent.i; \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/LockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/LockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..639d29128f3d0768b76afeb68331e77e4510d902 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/LockSimple.java @@ -0,0 +1,36 @@ +package fybug.nulll.pdconcurrent.i.simple; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryRunnable; +import fybug.nulll.pdconcurrent.fun.trySupplier; +import fybug.nulll.pdconcurrent.i.Lock; +import jakarta.validation.constraints.NotNull; + +/** + *

基础锁管理简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +public +interface LockSimple extends Lock { + /** + * 使用锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier, Function)}的简易变体 + */ + default + R lock(@NotNull LockType lockType, @NotNull trySupplier run) throws Exception + { return lock(lockType, run, null); } + + /** + * 使用锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable, Runnable)}的简易变体 + */ + default + void lock(@NotNull LockType lockType, @NotNull tryRunnable run) throws Exception + { lock(lockType, run, null); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/ReadLockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/ReadLockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..8515240bb3d6d1b4a02e49dc173c987cd3d1fa37 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/ReadLockSimple.java @@ -0,0 +1,35 @@ +package fybug.nulll.pdconcurrent.i.simple; +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryRunnable; +import fybug.nulll.pdconcurrent.fun.trySupplier; +import fybug.nulll.pdconcurrent.i.ReadLock; +import jakarta.validation.constraints.NotNull; + +/** + *

并发读锁支持简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +@SuppressWarnings("unused") +public +interface ReadLockSimple extends ReadLock { + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier)}指定读锁的变种 + */ + default + R read(@NotNull trySupplier run) throws Exception + { return lock(LockType.READ, run); } + + /** + * 使用读锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable)}指定读锁的变种 + */ + default + void read(@NotNull tryRunnable run) throws Exception + { lock(LockType.READ, run); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryLockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryLockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..8e8976a9ebeeb6629fa9304703166a2ffaec369d --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryLockSimple.java @@ -0,0 +1,36 @@ +package fybug.nulll.pdconcurrent.i.simple; +import java.util.function.Function; + +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryConsumer; +import fybug.nulll.pdconcurrent.fun.tryFunction; +import fybug.nulll.pdconcurrent.i.TryLock; +import jakarta.validation.constraints.NotNull; + +/** + *

TryLock 简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +public +interface TryLockSimple extends TryLock { + /** + * 尝试使用锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction, Function)}的简易变体 + */ + default + R trylock(@NotNull LockType lockType, @NotNull tryFunction run) throws Exception + { return trylock(lockType, run, null); } + + /** + * 尝试使用锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer, Runnable)}的简易变体 + */ + default + void trylock(@NotNull LockType lockType, @NotNull tryConsumer run) throws Exception + { trylock(lockType, run, null); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryReadLockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryReadLockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..fd495f2bf1fa2a618c1d736c246d04f32ba0ea0a --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryReadLockSimple.java @@ -0,0 +1,35 @@ +package fybug.nulll.pdconcurrent.i.simple; +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryConsumer; +import fybug.nulll.pdconcurrent.fun.tryFunction; +import fybug.nulll.pdconcurrent.i.TryReadLock; +import jakarta.validation.constraints.NotNull; + +/** + *

TryLock 读锁支持简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +@SuppressWarnings("unused") +public +interface TryReadLockSimple extends TryReadLock { + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction)}指定读锁的变种 + */ + default + R tryread(@NotNull tryFunction run) throws Exception + { return trylock(LockType.READ, run); } + + /** + * 尝试使用读锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer)}指定读锁的变种 + */ + default + void tryread(@NotNull tryConsumer run) throws Exception + { trylock(LockType.READ, run); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryWriteLockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryWriteLockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..914693614fd62c2d0e9ba185fbbcd05f1e224ff8 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/TryWriteLockSimple.java @@ -0,0 +1,35 @@ +package fybug.nulll.pdconcurrent.i.simple; +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryConsumer; +import fybug.nulll.pdconcurrent.fun.tryFunction; +import fybug.nulll.pdconcurrent.i.TryWriteLock; +import jakarta.validation.constraints.NotNull; + +/** + *

TryLock 写锁支持简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +@SuppressWarnings("unused") +public +interface TryWriteLockSimple extends TryWriteLock { + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryFunction)}指定写锁的变种 + */ + default + R trywrite(@NotNull tryFunction run) throws Exception + { return trylock(LockType.WRITE, run); } + + /** + * 尝试使用写锁执行指定回调 + *

+ * {@link #trylock(LockType, tryConsumer)}指定写锁的变种 + */ + default + void trywrite(@NotNull tryConsumer run) throws Exception + { trylock(LockType.WRITE, run); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/WriteLockSimple.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/WriteLockSimple.java new file mode 100644 index 0000000000000000000000000000000000000000..fbaa870f5537458c169d11d247009d4fae7c55de --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/WriteLockSimple.java @@ -0,0 +1,35 @@ +package fybug.nulll.pdconcurrent.i.simple; +import fybug.nulll.pdconcurrent.e.LockType; +import fybug.nulll.pdconcurrent.fun.tryRunnable; +import fybug.nulll.pdconcurrent.fun.trySupplier; +import fybug.nulll.pdconcurrent.i.WriteLock; +import jakarta.validation.constraints.NotNull; + +/** + *

并发写锁支持简易拓展.

+ * + * @author fybug + * @version 0.0.1 + * @since simple 0.0.1 + */ +@SuppressWarnings("unused") +public +interface WriteLockSimple extends WriteLock { + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, trySupplier)}指定写锁的变种 + */ + default + R write(@NotNull trySupplier run) throws Exception + { return lock(LockType.WRITE, run); } + + /** + * 使用写锁执行指定回调 + *

+ * {@link #lock(LockType, tryRunnable)}指定写锁的变种 + */ + default + void write(@NotNull tryRunnable run) throws Exception + { lock(LockType.WRITE, run); } +} diff --git a/src/main/java/fybug/nulll/pdconcurrent/i/simple/package-info.java b/src/main/java/fybug/nulll/pdconcurrent/i/simple/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..9b0b93ad4af265099b33ecb2420c63383e108b69 --- /dev/null +++ b/src/main/java/fybug/nulll/pdconcurrent/i/simple/package-info.java @@ -0,0 +1,9 @@ +/** + *

接口简易拓展包.

+ * 提供部分方法简易变体的拓展接口 + * + * @author fybug + * @version 0.0.1 + * @since PDConcurrent 0.1.3 + */ +package fybug.nulll.pdconcurrent.i.simple; \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdconcurrent/lock/RWLock.java b/src/main/java/fybug/nulll/pdconcurrent/lock/RWLock.java index ea25e360c859acb7df248d5ad048cc29df67929b..4845fad60ecad33f42598af791d411c4577a9803 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/lock/RWLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/lock/RWLock.java @@ -195,7 +195,7 @@ class RWLock extends AbstractSyLock { // 更新对应锁计数 if ( lockType == LockType.READ ) READ_LOCK_COUNTER.getAndIncrement(); - else //noinspection ConstantValue + else // noinspection ConstantValue if ( lockType == LockType.WRITE ) WRITE_LOCK_COUNTER.getAndIncrement(); } else if ( l.isEmpty() ) { diff --git a/src/main/java/fybug/nulll/pdconcurrent/package-info.java b/src/main/java/fybug/nulll/pdconcurrent/package-info.java index bcd81f6be4c25d43de5dcef4982a99802c4e0583..53f2a469ed36b9c03df91291bdff9d5e9a9b82fa 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/package-info.java +++ b/src/main/java/fybug/nulll/pdconcurrent/package-info.java @@ -5,7 +5,7 @@ * 附带{@code try***}的功能接口包{@link fybug.nulll.pdconcurrent.fun},在java原有的功能接口的基础上允许抛出异常 * * @author fybug - * @version 0.1.2 + * @version 0.1.3 * @since JDK 23+ */ package fybug.nulll.pdconcurrent; \ No newline at end of file