diff --git a/README.md b/README.md index 49817e3c447a20638c7b4dac9a8056fbc1b74d9c..f387e90b49704a1bf91a014b7436a5233923cb1f 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,12 @@ ![Gradle 8.10.2](https://img.shields.io/badge/Gradle-8.10.2-04303b.svg "tool") ![Apache 2](https://img.shields.io/badge/license-Apache%202-blue.svg "License") --- [Java Doc](https://apidoc.gitee.com/fybug/PDConcurrent) -- +[//]: # (-- [Java Doc](https://apidoc.gitee.com/fybug/PDConcurrent) --) --- [项目主页](https://fybug.gitee.io/projectsby/PDConcurrent.html) -- +[//]: # (-- [项目主页](https://fybug.gitee.io/projectsby/PDConcurrent.html) --) ------------------------------------------------------------------------------- -**专注于单个功能,无附加bug** - ## 简介 轻量级,高可靠,易学习的并发管理工具。 @@ -24,12 +22,15 @@ 如果你已经熟悉 java 的多线程管理,尤其是 ReentrantReadWriteLock 读写锁的话你会更容易学会本工具。 -本工具采用读写锁标准,无论内部实现如何,外部接口均提供 **read, **write 方法区分读锁并发域和写锁并发域 +无论内部实现如何,外部接口均提供 **read, **write 方法区分读锁并发域和写锁并发域 ### 基础使用示例 + ```java -public static void main(String[] args) { - var lock = SyLock.newObjLock(); +public final SyLock lock = SyLock.newObjLock(); + +public static +void main(String[] args) { lock.read(() -> { // 并发域代码内容 [return null;]? // 可选择是否返回 @@ -46,10 +47,13 @@ public static void main(String[] args) { 可通过接口的工厂方法实例化内部采用不同实现的锁。 ### synchronized 锁 + ```java -public static void main(String[] args) { +public final SyLock lock = SyLock.newObjLock(); + +public static +void main(String[] args) { // 使用 - var lock = SyLock.newObjLock(); lock.read(() -> { // 并发域代码内容 }); @@ -63,10 +67,16 @@ public static void main(String[] args) { ``` ### ReentrantLock 锁 + +支持使用`newCondition()`获取`Condition`对象 + +支持通过`isLocked()`检查是否被占用 + ```java -public static void main(String[] args) { +public static +void main(String[] args) { // 使用 - var lock = SyLock.newReLock(); + SyLock lock = SyLock.newReLock(); lock.read(() -> { // 并发域代码内容 }); @@ -83,10 +93,18 @@ public static void main(String[] args) { ``` ### ReadWriteLock 锁 + +支持通过`toread()`进行写锁 => 读锁降级 + +支持使用`newReadCondition()` `newWriteCondition()`获取`Condition`对象 + +支持通过`isLocked()` `isWriteLocked()` `isReadLocked()`检查是否被占用 + ```java -public static void main(String[] args) { +public static +void main(String[] args) { // 使用 - var lock = SyLock.newRWLock(); + SyLock lock = SyLock.newRWLock(); lock.read(() -> { // 并发域代码内容 }); @@ -103,17 +121,19 @@ public static void main(String[] args) { ``` ## 使用方法 + 请导入其 `jar` 文件,文件在 **发行版** 或项目的 **jar** 文件夹下可以找到 -> PDConcurrent.jar 为不包含源码的包 +> PDConcurrent_bin.jar 为编译包 > -> PDConcurrent_sources.jar 为仅包含源码的包 +> PDConcurrent_sources.jar 为源码包 **发行版中可以看到全部版本
项目下的 jar 文件夹是当前最新的每夜版** 可通过 **WIKI** 或者 **doc文档** 深入学习本工具 ## 分支说明 + **dev-master**:当前的开发分支,可以拿到最新的每夜版 jar **releases**:当前发布分支,稳定版的源码 diff --git a/build.gradle b/build.gradle index 37a7b5ac91038607335cd537f1e9c310b61260f4..bceb43517581372e06266cf332d095e190041647 100644 --- a/build.gradle +++ b/build.gradle @@ -1,116 +1,113 @@ plugins { - id 'java' - id 'idea' + id 'java' + id 'idea' } tasks.withType(JavaCompile).configureEach { - options.encoding = "UTF-8" + options.encoding = "UTF-8" } group 'fybug.nulll' version = '0.1.0' java { - toolchain { - languageVersion = JavaLanguageVersion.of(23) - } + toolchain { + languageVersion = JavaLanguageVersion.of(23) + } } repositories { - mavenLocal() - mavenCentral() - google() - maven { url 'https://maven.aliyun.com/repository/releases' } - maven { url "https://maven.aliyun.com/repository/jcenter" } - maven { url "https://maven.aliyun.com/repository/mapr-public" } - maven { url "https://maven.aliyun.com/repository/staging-alpha" } - maven { url "https://maven.aliyun.com/repository/central" } - maven { url "https://maven.aliyun.com/repository/public/" } - maven { url "https://maven.aliyun.com/repository/google" } - maven { url "https://maven.aliyun.com/repository/gradle-plugin" } - maven { url "https://maven.aliyun.com/repository/spring" } - maven { url "https://maven.aliyun.com/repository/spring-plugin" } - maven { url "https://maven.aliyun.com/repository/grails-core" } - maven { url "https://maven.aliyun.com/repository/snapshots" } - maven { url "https://maven.aliyun.com/repository/apache-snapshots" } - maven { url "https://maven.aliyun.com/repository/staging-alpha-group" } + mavenLocal() + mavenCentral() + google() + maven { url 'https://maven.aliyun.com/repository/releases' } + maven { url "https://maven.aliyun.com/repository/jcenter" } + maven { url "https://maven.aliyun.com/repository/mapr-public" } + maven { url "https://maven.aliyun.com/repository/staging-alpha" } + maven { url "https://maven.aliyun.com/repository/central" } + maven { url "https://maven.aliyun.com/repository/public/" } + maven { url "https://maven.aliyun.com/repository/google" } + maven { url "https://maven.aliyun.com/repository/gradle-plugin" } + maven { url "https://maven.aliyun.com/repository/spring" } + maven { url "https://maven.aliyun.com/repository/spring-plugin" } + maven { url "https://maven.aliyun.com/repository/grails-core" } + maven { url "https://maven.aliyun.com/repository/snapshots" } + maven { url "https://maven.aliyun.com/repository/apache-snapshots" } + maven { url "https://maven.aliyun.com/repository/staging-alpha-group" } } dependencies { - // 本地依赖 - implementation fileTree(dir: 'lib', includes: ['*.jar']) + // 本地依赖 + implementation fileTree(dir: 'lib', includes: ['*.jar']) - // 注释包 - compileOnly "jakarta.validation:jakarta.validation-api:+" - compileOnly "jakarta.annotation:jakarta.annotation-api:+" - // lombok - compileOnly 'org.projectlombok:lombok:+' - annotationProcessor 'org.projectlombok:lombok:+' + // 注释包 + compileOnly "jakarta.validation:jakarta.validation-api:+" + compileOnly "jakarta.annotation:jakarta.annotation-api:+" + // lombok + compileOnly 'org.projectlombok:lombok:+' + annotationProcessor 'org.projectlombok:lombok:+' - // 注释包 - testCompileOnly "jakarta.validation:jakarta.validation-api:+" - testCompileOnly "jakarta.annotation:jakarta.annotation-api:+" - // lombok - testCompileOnly "org.projectlombok:lombok:+" - testAnnotationProcessor "org.projectlombok:lombok:+" - // testImplementation "junit:junit" + // 注释包 + testCompileOnly "jakarta.validation:jakarta.validation-api:+" + testCompileOnly "jakarta.annotation:jakarta.annotation-api:+" + // lombok + testCompileOnly "org.projectlombok:lombok:+" + testAnnotationProcessor "org.projectlombok:lombok:+" + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } test { - useJUnitPlatform() + dependencies { + testImplementation "org.junit.platform:junit-platform-launcher" + } + useJUnitPlatform() } /** 清单文件内容 */ tasks.withType(Jar).configureEach { - manifest { - attributes('Manifest-Version': '1.0', - 'Built-By': 'fybug/风雨bu改', - 'Build-Jdk-Spec': 23, - 'Bundle-Description': 'java并发控制工具', - 'Bundle-Name': 'PDConcurrent', - // 'Bundle-DocURL': 'https://apidoc.gitee.com/fybug/PDConcurrent/', - 'Bundle-Vendor': 'IntelliJ IDEA', - 'Bundle-Version': version, - 'Bundle-License': 'https://www.apache.org/licenses/LICENSE-2.0', - 'Created-By': 'Gradle 8.10.2') - } + destinationDirectory = file('jar') + manifest { + attributes('Manifest-Version': '1.0', + 'Built-By': 'fybug/风雨bu改', + 'Build-Jdk-Spec': 23, + 'Bundle-Description': 'java并发控制工具', + 'Bundle-Name': 'PDConcurrent', + // 'Bundle-DocURL': 'https://apidoc.gitee.com/fybug/PDConcurrent/', + 'Bundle-Vendor': 'IntelliJ IDEA', + 'Bundle-Version': version, + 'Bundle-License': 'https://www.apache.org/licenses/LICENSE-2.0', + 'Created-By': 'Gradle 8.10.2') + } } /** 编译包 */ tasks.register('PDConcurrent_bin', Jar) { - dependsOn classes - destinationDirectory = file('jar') - archiveFileName = 'PDConcurrent_bin.jar' - archiveClassifier = '' - // 打包编译输出 - from sourceSets.main.output + archiveFileName = 'PDConcurrent_bin.jar' + archiveClassifier = '' + // 打包编译输出 + from sourceSets.main.output } /** 源码包 */ tasks.register('PDConcurrent_sources', Jar) { - dependsOn classes - destinationDirectory = file('jar') - archiveFileName = 'PDConcurrent_sources.jar' - archiveClassifier = 'sources' - // 打包源码 - from sourceSets.main.allSource + archiveFileName = 'PDConcurrent_sources.jar' + archiveClassifier = 'sources' + // 打包源码 + from sourceSets.main.allSource } -tasks.register('PDConcurrent_all', Jar) { - dependsOn classes - destinationDirectory = file('jar') - archiveFileName = 'PDConcurrent_all.jar' - archiveClassifier = 'all' - // 打包编译输出 - from sourceSets.main.output - from { - // implementation 相关的引入解压并打包入新的jar中 - configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } - } -} +//tasks.register('PDConcurrent_all', Jar) { +// archiveFileName = 'PDConcurrent_all.jar' +// archiveClassifier = 'all' +// // 打包编译输出 +// from sourceSets.main.output +// from { +// // implementation 相关的引入解压并打包入新的jar中 +// configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } +// } +//} tasks.register('release') { - dependsOn clean - dependsOn PDConcurrent_bin - dependsOn PDConcurrent_sources + dependsOn PDConcurrent_bin + dependsOn PDConcurrent_sources } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index df97d72b8b91fa0e088ae27b1c84a6063481fe22..e0dfef4284c2074f9674dc5a45dd2f4334c247f3 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip +distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.10.2-bin.zip +#distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/jar/PDConcurrent_bin.jar b/jar/PDConcurrent_bin.jar index 3e78b12bf035f2071850f0ea33c68fc1d9007403..b8cac15ec28adeb50b1b857d8b05d8a9bfd79d90 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 ecd6bd634795df52c186da40841d6dc8dc4d9469..faaffcee6bc8121a4c9264a801a03ab9485cee1a 100644 Binary files a/jar/PDConcurrent_sources.jar and b/jar/PDConcurrent_sources.jar differ diff --git a/src/main/java/fybug/nulll/pdconcurrent/ObjLock.java b/src/main/java/fybug/nulll/pdconcurrent/ObjLock.java index d36d99e2036d341ddf59b6f388ebc4dda0b00e7e..9f138ec391d75dc908c7110d7b2076d98737d598 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/ObjLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/ObjLock.java @@ -13,14 +13,14 @@ import lombok.Getter; *

* 使用并发管理: * {@snippet lang = java: + * public final SyLock lock = new ObjLock(); * public static void main(String[] args) { - * var lock = new ObjLock(); * lock.read(() -> System.out.println("asd")); * }} * 不使用: * {@snippet lang = java: + * public final Object lock = new Object(); * public static void main(String[] args) { - * Object lock = new Object(); * synchronized ( lock ){ * System.out.println("asd"); * } @@ -34,125 +34,123 @@ import lombok.Getter; @Getter public class ObjLock implements SyLock { - /** 作为锁的对象 */ - private final Object LOCK; + /** 作为锁的对象 */ + private final Object LOCK; - /** - * 构建并发管理 - *

- * 使用一个新的{@link Object} - */ - public - ObjLock() { this(new Object()); } + /** + * 构建并发管理 + *

+ * 使用一个新的{@link Object} + */ + public + ObjLock() { this(new Object()); } - /** - * 构建并发管理 - * - * @param lock 用作锁的对象 - * - * @since 0.1.0 - */ - public - ObjLock(@NotNull Object lock) { LOCK = lock; } + /** + * 构建并发管理 + * + * @param lock 用作锁的对象 + * + * @since 0.1.0 + */ + public + ObjLock(@NotNull Object lock) { LOCK = lock; } - //---------------------------------------------------------------------------------------------- + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param catchby {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@code synchronized( Object )} 实现的隐式并发域 + * @see SyLock#lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + @Override + public + R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, + @Nullable Function finaby) + { + R o = null; + // 不上锁 + if ( lockType == LockType.NOLOCK ) { + try { + // 主要内容 + o = run.get(); + } catch ( Exception e ) { + // 异常处理 + if ( catchby != null ) + o = catchby.apply(e); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } else { + // 上锁 + synchronized ( LOCK ){ + try { + // 主要内容 + o = run.get(); + } catch ( Exception e ) { + // 异常处理 + if ( catchby != null ) + o = catchby.apply(e); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } + } + return o; + } - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param catchby {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@code synchronized( Object )} 实现的隐式并发域 - * @see SyLock#lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - @Override - public - R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, - @Nullable Function finaby) - { - R o = null; - // 不上锁 - if ( lockType == LockType.NOLOCK ) { - try { - // 主要内容 - o = run.get(); - } catch ( Exception e ) { - // 异常处理 - if ( catchby != null ) - o = catchby.apply(e); - } finally { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } - } else { - // 上锁 - synchronized ( LOCK ){ - try { - // 主要内容 - o = run.get(); - } catch ( Exception e ) { - // 异常处理 - if ( catchby != null ) - o = catchby.apply(e); - } finally { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } - } - } - return o; - } - - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@code synchronized( Object )} 实现的隐式并发域 - * @see SyLock#trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - @Override - public - R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { - R o = null; - // 不上锁 - if ( lockType == LockType.NOLOCK ) { - try { - // 主要内容 - o = run.get(); - } finally { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } - } else { - // 上锁 - synchronized ( LOCK ){ - try { - // 主要内容 - o = run.get(); - } finally { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } - } - } - return o; - } + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@code synchronized( Object )} 实现的隐式并发域 + * @see SyLock#trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + @Override + public + R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { + R o = null; + // 不上锁 + if ( lockType == LockType.NOLOCK ) { + try { + // 主要内容 + o = run.get(); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } else { + // 上锁 + synchronized ( LOCK ){ + try { + // 主要内容 + o = run.get(); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } + } + return o; + } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/RWLock.java b/src/main/java/fybug/nulll/pdconcurrent/RWLock.java index 6cc72556a638afa7185c26768a56a68fd424a53b..e5ef8d5802b9da5694e8fb3b4c0f6397ad6b836d 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/RWLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/RWLock.java @@ -17,8 +17,8 @@ import lombok.Getter; *

* 使用并发管理: * {@snippet lang = java: + * public final SyLock lock = new RWLock(); * public static void main(String[] args) { - * var lock = new RWLock(); * // 使用读锁 * lock.read(() -> System.out.println("adsa")); * // 使用写锁 @@ -27,14 +27,14 @@ import lombok.Getter; * 不使用: * {@snippet lang = java: * import java.util.concurrent.locks.ReentrantReadWriteLock; + * public final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); * public static void main(String[] args) { - * var lock = new ReentrantReadWriteLock(); * // 使用读锁 * try { * lock.readLock().lock(); * System.out.println("adsa"); * } finally { - * lock.readLock().unlock(); + * lock.readLock().unlock(); * } * // 使用写锁 * try { @@ -57,264 +57,261 @@ import lombok.Getter; @Getter public class RWLock implements SyLock { - /** 锁 */ - private final ReentrantReadWriteLock LOCK; - /** 读锁 */ - private final ReentrantReadWriteLock.ReadLock Read_LOCK; - /** 写锁 */ - private final ReentrantReadWriteLock.WriteLock Write_LOCK; - /** - * 每个线程的锁状态记录 - *

- * 读锁为{@code 1}
- * 写锁为{@code 2} - */ - private final ThreadLocal LOCK_STATE = new ThreadLocal<>(); + /** 锁 */ + private final ReentrantReadWriteLock LOCK; + /** 读锁 */ + private final ReentrantReadWriteLock.ReadLock Read_LOCK; + /** 写锁 */ + private final ReentrantReadWriteLock.WriteLock Write_LOCK; + /** + * 每个线程的锁状态记录 + *

+ * 不上锁为{@code null}
+ * 读锁为{@code 1}
+ * 写锁为{@code 2} + */ + private final ThreadLocal LOCK_STATE = new ThreadLocal<>(); - /** - * 构建并发管理 - *

- * 使用非公平锁 - */ - public - RWLock() { this(false); } + /** + * 构建并发管理 + *

+ * 使用非公平锁 + */ + public + RWLock() { this(false); } - /** - * 构造并发处理 - * - * @param fair 是否使用公平锁 - */ - public - RWLock(boolean fair) { this(new ReentrantReadWriteLock(fair)); } + /** + * 构造并发处理 + * + * @param fair 是否使用公平锁 + */ + public + RWLock(boolean fair) { this(new ReentrantReadWriteLock(fair)); } - /** - * 构造并发处理 - * - * @param lock 使用的锁 - * - * @since 0.1.0 - */ - public - RWLock(@NotNull ReentrantReadWriteLock lock) { - LOCK = lock; - Read_LOCK = LOCK.readLock(); - Write_LOCK = LOCK.writeLock(); - } + /** + * 构造并发处理 + * + * @param lock 使用的锁 + * + * @since 0.1.0 + */ + public + RWLock(@NotNull ReentrantReadWriteLock lock) { + LOCK = lock; + Read_LOCK = LOCK.readLock(); + Write_LOCK = LOCK.writeLock(); + } - //---------------------------------------------------------------------------------------------- + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param catchby {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@link ReentrantReadWriteLock} 实现的并发域,上锁通过{@link #tolock(LockType)}进行 + * @see SyLock#lock(LockType, trySupplier, Function, Function) + * @see #tolock(LockType) + * @see #tounlock() + * @since 0.1.0 + */ + @Override + public + R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, + @Nullable Function finaby) + { + R o = null; + // 清空当前线程的状态 + LOCK_STATE.remove(); + // 防止finally内的回调抛异常 + try { + try { + // 上锁 + tolock(lockType); + // 主要内容 + o = run.get(); + } catch ( Exception e ) { + // 异常处理 + if ( catchby != null ) + o = catchby.apply(e); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } finally { + // 解锁 + tounlock(); + } + return o; + } - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param catchby {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@link ReentrantReadWriteLock} 实现的并发域,上锁通过{@link #tolock(LockType)}进行 - * @see SyLock#lock(LockType, trySupplier, Function, Function) - * @see #tolock(LockType) - * @see #tounlock() - * @since 0.1.0 - */ - @Override - public - R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, - @Nullable Function finaby) - { - R o = null; - // 清空当前线程的状态 - LOCK_STATE.remove(); - try { - // 上锁 - tolock(lockType); - // 主要内容 - o = run.get(); - } catch ( Exception e ) { - // 异常处理 - if ( catchby != null ) - o = catchby.apply(e); - } finally { - // 防止错误 - try { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } finally { - // 解锁 - tounlock(); - } - } - return o; - } + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@link ReentrantReadWriteLock} 实现的并发域,上锁通过{@link #tolock(LockType)}进行 + * @see SyLock#trylock(LockType, trySupplier, Function) + * @see #tolock(LockType) + * @see #tounlock() + * @since 0.1.0 + */ + @Override + public + R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { + R o = null; + // 清空当前线程的状态 + LOCK_STATE.remove(); + // 防止finally内的回调抛异常 + try { + try { + // 上锁 + tolock(lockType); + // 主要内容 + o = run.get(); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } finally { + // 解锁 + tounlock(); + } + return o; + } - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@link ReentrantReadWriteLock} 实现的并发域,上锁通过{@link #tolock(LockType)}进行 - * @see SyLock#trylock(LockType, trySupplier, Function) - * @see #tolock(LockType) - * @see #tounlock() - * @since 0.1.0 - */ - @Override - public - R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { - R o = null; - // 清空当前线程的状态 - LOCK_STATE.remove(); - try { - // 上锁 - tolock(lockType); - // 主要内容 - o = run.get(); - } finally { - // 防止错误 - try { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } finally { - // 解锁 - tounlock(); - } - } - return o; - } + /** + * 根据指定类型上锁 + *

+ * 使用了可中断的上锁操作{@link ReentrantReadWriteLock.ReadLock#lockInterruptibly()}和{@link ReentrantReadWriteLock.WriteLock#lockInterruptibly()}
+ * 同时更新{@link #LOCK_STATE}记录 + * + * @param lockType 锁类型 + * + * @throws InterruptedException 上锁过程中被中断 + * @see #LOCK_STATE + * @see LockType + * @since 0.1.0 + */ + private + void tolock(@NotNull LockType lockType) throws InterruptedException { + if ( lockType != LockType.NOLOCK ) { + if ( lockType == LockType.READ ) { + // 读锁 + Read_LOCK.lockInterruptibly(); + LOCK_STATE.set((short) 1); + } else if ( lockType == LockType.WRITE ) { + // 写锁 + Write_LOCK.lockInterruptibly(); + LOCK_STATE.set((short) 2); + } + } + } - /** - * 根据指定类型上锁 - *

- * 使用了可中断的上锁操作{@link ReentrantReadWriteLock.ReadLock#lockInterruptibly()}和{@link ReentrantReadWriteLock.WriteLock#lockInterruptibly()}
- * 同时更新{@link #LOCK_STATE}记录 - * - * @param lockType 锁类型 - * - * @throws InterruptedException 上锁过程中被中断 - * @see #LOCK_STATE - * @see LockType - * @since 0.1.0 - */ - private - void tolock(@NotNull LockType lockType) throws InterruptedException { - if ( lockType != LockType.NOLOCK ) { - if ( lockType == LockType.READ ) { - // 读锁 - Read_LOCK.lockInterruptibly(); - LOCK_STATE.set((short) 1); - } else { - // 写锁 - Write_LOCK.lockInterruptibly(); - LOCK_STATE.set((short) 2); - } - } - } + /** + * 根据状态解锁 + *

+ * 根据{@link #LOCK_STATE}的状态调用对应的解锁动作
+ * 成功后清空{@link #LOCK_STATE}内容 + * + * @see #LOCK_STATE + * @since 0.1.0 + */ + private + void tounlock() { + // 根据实际状态解锁 + if ( LOCK_STATE.get() == 1 ) + Read_LOCK.unlock(); + else if ( LOCK_STATE.get() == 2 ) + Write_LOCK.unlock(); + // 清除记录数据 + LOCK_STATE.remove(); + } - /** - * 根据状态解锁 - *

- * 根据{@link #LOCK_STATE}的状态调用对应的解锁动作
- * 成功后清空{@link #LOCK_STATE}内容 - * - * @see #LOCK_STATE - * @since 0.1.0 - */ - private - void tounlock() { - // 根据实际状态解锁 - if ( LOCK_STATE.get() == 1 ) - Read_LOCK.unlock(); - else if ( LOCK_STATE.get() == 2 ) - Write_LOCK.unlock(); - // 清除记录数据 - LOCK_STATE.remove(); - } + /** + * 转为读锁 + *

+ * 如果当前状态为写锁则会降级为读锁,否则不进行操作 + * + * @return 是否成功降级 + * + * @see #LOCK_STATE + * @since 0.1.0 + */ + public + boolean toread() { + // 转为读锁 + if ( LOCK_STATE.get() == 2 ) { + Read_LOCK.lock(); + Write_LOCK.unlock(); + LOCK_STATE.set((short) 1); + return true; + } + return false; + } - /** - * 转为读锁 - *

- * 如果当前状态为写锁则会降级为读锁,否则不进行操作 - * - * @return 是否成功降级 - * - * @see #LOCK_STATE - * @since 0.1.0 - */ - public - boolean toread() { - // 转为读锁 - if ( LOCK_STATE.get() == 2 ) { - Read_LOCK.lock(); - Write_LOCK.unlock(); - LOCK_STATE.set((short) 1); - return true; - } - return false; - } + /** + * 检查锁是否被占用 + * + * @return 是否被占用 + * + * @since 0.1.0 + */ + public + boolean isLocked() { return isReadLocked() || isWriteLocked(); } - //---------------------------------------------------------------------------------------------- + /** + * 检查读锁是否被占用 + * + * @return 是否被占用 + * + * @since 0.1.0 + */ + public + boolean isReadLocked() { return LOCK_STATE.get() == 1; } - /** - * 检查锁是否被占用 - * - * @return 是否被占用 - * - * @since 0.1.0 - */ - public - boolean isLocked() { return LOCK_STATE.get() != null && (LOCK_STATE.get() == 1 || LOCK_STATE.get() == 2); } + /** + * 检查写锁是否被占用 + * + * @return 是否被占用 + * + * @since 0.1.0 + */ + public + boolean isWriteLocked() { return LOCK_STATE.get() == 2; } - /** - * 检查读锁是否被占用 - * - * @return 是否被占用 - * - * @since 0.1.0 - */ - public - boolean isReadLocked() { return LOCK_STATE.get() == 1; } + /** + * 获取读锁{@link Condition} + * + * @return {@link ReentrantReadWriteLock.ReadLock}的{@link Condition} + * + * @see ReentrantReadWriteLock.ReadLock#newCondition() + * @since 0.1.0 + */ + @NotNull + public + Condition newReadCondition() { return Read_LOCK.newCondition(); } - /** - * 检查写锁是否被占用 - * - * @return 是否被占用 - * - * @since 0.1.0 - */ - public - boolean isWriteLocked() { return LOCK_STATE.get() == 2; } - - /** - * 获取读锁{@link Condition} - * - * @return {@link ReentrantReadWriteLock.ReadLock}的{@link Condition} - * - * @see ReentrantReadWriteLock.ReadLock#newCondition() - * @since 0.1.0 - */ - @NotNull - public - Condition newReadCondition() { return Read_LOCK.newCondition(); } - - /** - * 获取写锁{@link Condition} - * - * @return {@link ReentrantReadWriteLock.WriteLock}的{@link Condition} - * - * @see ReentrantReadWriteLock.WriteLock#newCondition() - * @since 0.1.0 - */ - @NotNull - public - Condition newWriteCondition() { return Write_LOCK.newCondition(); } + /** + * 获取写锁{@link Condition} + * + * @return {@link ReentrantReadWriteLock.WriteLock}的{@link Condition} + * + * @see ReentrantReadWriteLock.WriteLock#newCondition() + * @since 0.1.0 + */ + @NotNull + public + Condition newWriteCondition() { return Write_LOCK.newCondition(); } } \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdconcurrent/ReLock.java b/src/main/java/fybug/nulll/pdconcurrent/ReLock.java index fbf385b0216d325b47289a4c5b68c9c6e0cc6a56..8defe87a0e2d7e71a7607d008962877a8512b2fc 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/ReLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/ReLock.java @@ -17,17 +17,18 @@ import lombok.Getter; *

* 使用并发管理: * {@snippet lang = java: + * public final SyLock lock = new ReLock(); * public static void main(String[] args) { - * var lock = new ReLock(); * lock.read(() -> System.out.println("asdas")); * }} * 不使用: * {@snippet lang = java: * import java.util.concurrent.locks.ReentrantLock; + * + * public final ReentrantLock lock = new ReentrantLock(); * public static void main(String[] args) { - * var lock = new ReentrantLock(); - * lock.lock(); * try { + * lock.lock(); * System.out.println("asdas"); * } finally { * lock.unlock(); @@ -43,140 +44,136 @@ import lombok.Getter; @Getter public class ReLock implements SyLock { - /** 锁 */ - private final ReentrantLock LOCK; - - /** - * 构建并发管理 - *

- * 使用非公平锁 - */ - public - ReLock() { this(false); } - - /** - * 构造并发处理 - * - * @param fair 是否使用公平锁 - */ - public - ReLock(boolean fair) { this(new ReentrantLock(fair)); } + /** 锁 */ + private final ReentrantLock LOCK; - /** - * 构造并发处理 - * - * @param LOCK 使用的锁 - * - * @since 0.1.0 - */ - public - ReLock(@NotNull ReentrantLock LOCK) { this.LOCK = LOCK; } + /** + * 构建并发管理 + *

+ * 使用非公平锁 + */ + public + ReLock() { this(false); } - //---------------------------------------------------------------------------------------------- + /** + * 构造并发处理 + * + * @param fair 是否使用公平锁 + */ + public + ReLock(boolean fair) { this(new ReentrantLock(fair)); } - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param catchby {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@link ReentrantLock} 实现的并发域,使用了{@link ReentrantLock#lockInterruptibly()}进行可中断的上锁操作 - * @see SyLock#lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - @Override - public - R lock(@NotNull LockType lockType, trySupplier run, @Nullable Function catchby, - @Nullable Function finaby) - { - R o = null; - try { - // 上锁 - if ( lockType != LockType.NOLOCK ) - LOCK.lockInterruptibly(); - // 主要内容 - o = run.get(); - } catch ( Exception e ) { - // 异常处理 - if ( catchby != null ) - o = catchby.apply(e); - } finally { - // 防止错误 - try { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } finally { - // 解锁 - if ( lockType != LockType.NOLOCK && LOCK.isLocked() ) - LOCK.unlock(); - } - } - return o; - } + /** + * 构造并发处理 + * + * @param LOCK 使用的锁 + * + * @since 0.1.0 + */ + public + ReLock(@NotNull ReentrantLock LOCK) { this.LOCK = LOCK; } - /** - * {@inheritDoc} - * - * @param lockType {@inheritDoc} - * @param run {@inheritDoc} - * @param finaby {@inheritDoc} - * @param {@inheritDoc} - * - * @return {@inheritDoc} - * - * @implNote 使用 {@link ReentrantLock} 实现的并发域,使用了{@link ReentrantLock#lockInterruptibly() - * @see SyLock#trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - @Override - public - R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { - R o = null; - try { - // 上锁 - if ( lockType != LockType.NOLOCK ) - LOCK.lockInterruptibly(); - // 主要内容 - o = run.get(); - } finally { - // 防止错误 - try { - // 收尾 - if ( finaby != null ) - o = finaby.apply(o); - } finally { - // 解锁 - if ( lockType != LockType.NOLOCK && LOCK.isLocked() ) - LOCK.unlock(); - } - } - return o; - } + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param catchby {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@link ReentrantLock} 实现的并发域,使用了{@link ReentrantLock#lockInterruptibly()}进行可中断的上锁操作 + * @see SyLock#lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + @Override + public + R lock(@NotNull LockType lockType, trySupplier run, @Nullable Function catchby, + @Nullable Function finaby) + { + R o = null; + // 防止finally内的回调抛异常 + try { + try { + // 上锁 + if ( lockType != LockType.NOLOCK ) + LOCK.lockInterruptibly(); + // 主要内容 + o = run.get(); + } catch ( Exception e ) { + // 异常处理 + if ( catchby != null ) + o = catchby.apply(e); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } finally { + // 解锁 + if ( lockType != LockType.NOLOCK && LOCK.isLocked() ) + LOCK.unlock(); + } + return o; + } - //---------------------------------------------------------------------------------------------- + /** + * {@inheritDoc} + * + * @param lockType {@inheritDoc} + * @param run {@inheritDoc} + * @param finaby {@inheritDoc} + * @param {@inheritDoc} + * + * @return {@inheritDoc} + * + * @implNote 使用 {@link ReentrantLock} 实现的并发域,使用了{@link ReentrantLock#lockInterruptibly() + * @see SyLock#trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + @Override + public + R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception { + R o = null; + // 防止finally内的回调抛异常 + try { + try { + // 上锁 + if ( lockType != LockType.NOLOCK ) + LOCK.lockInterruptibly(); + // 主要内容 + o = run.get(); + } finally { + // 收尾 + if ( finaby != null ) + o = finaby.apply(o); + } + } finally { + // 解锁 + if ( lockType != LockType.NOLOCK && LOCK.isLocked() ) + LOCK.unlock(); + } + return o; + } - /** - * 获取{@link Condition} - * - * @return {@link ReentrantLock}的{@link Condition} - * - * @see ReentrantLock#newCondition() - */ - @NotNull - public - Condition newCondition() { return LOCK.newCondition(); } + /** + * 获取{@link Condition} + * + * @return {@link ReentrantLock}的{@link Condition} + * + * @see ReentrantLock#newCondition() + */ + @NotNull + public + Condition newCondition() { return LOCK.newCondition(); } - /** - * 检查锁是否被占用 - * - * @return 是否被占用 - */ - public - boolean isLocked() { return LOCK.isLocked(); } + /** + * 检查锁是否被占用 + * + * @return 是否被占用 + */ + public + boolean isLocked() { return LOCK.isLocked(); } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/SyLock.java b/src/main/java/fybug/nulll/pdconcurrent/SyLock.java index 4526b1a73055d9634db49dd2d2df752e1ac2fc45..113455068eec4dc9ff6655b39c62ab13b0fd9a87 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/SyLock.java +++ b/src/main/java/fybug/nulll/pdconcurrent/SyLock.java @@ -18,513 +18,541 @@ import jakarta.validation.constraints.NotNull; * 使用 {@code new**Lock()} 的方法获取不同并发管理的实例
* * @author fybug - * @version 0.1.0 - * @apiNote 并发控制通用接口,规定并实现大部分通用控制功能 + * @version 0.1.1 * @since PDConcurrent 0.0.1 */ public interface SyLock { - /** - * 使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param lockType 锁类型 - * @param run 带返回的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @implSpec 如果有传入 {@code finaby} 回调则返回值由{@code finaby}主导,传入{@code finaby}的值根据是否发生异常传入{@code run}的返回值或{@code catchby}的返回值
- * 任意一个回调为空时直接穿透,使用上一个正确执行的值进行传递或者返回,返回应有默认值{@code null}用于应对{@code catchby}和{@code finaby}都为空但是发生了异常的情况 - * @see trySupplier - * @see Function - * @see LockType - * @since 0.1.0 - */ - R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, - @Nullable Function finaby); + /** + * 使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param lockType 锁类型 + * @param run 带返回的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调,传入前两个回调的返回值 + * @param 要返回的数据类型 + * + * @return 回调返回的内容 + * + * @implSpec 如果有传入 {@code finaby} 回调则返回值由{@code finaby}主导,传入{@code finaby}的值根据是否发生异常传入{@code run}的返回值或{@code catchby}的返回值
+ * 任意一个回调为空时直接穿透,使用上一个正确执行的值进行传递或者返回,传递值应默认为{@code null}用于应对{@code catchby}和{@code finaby}都为空但是发生了异常的情况 + * @see trySupplier + * @see Function + * @see LockType + * @since 0.1.0 + */ + R lock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function catchby, + @Nullable Function finaby); - /** - * 使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - * - * @see tryRunnable - * @see Consumer - * @see Runnable - * @see LockType - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void lock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Consumer catchby, - @Nullable Runnable finaby) - { - lock(lockType, () -> { - run.run(); - return null; - }, catchby == null ? null : e -> { - catchby.accept(e); - return null; - }, finaby == null ? null : _ -> { - finaby.run(); - return null; - }); - } + /** + * 使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param lockType 锁类型 + * @param run 执行的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调 + * + * @see tryRunnable + * @see Consumer + * @see Runnable + * @see LockType + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + void lock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Consumer catchby, + @Nullable Runnable finaby) + { + lock(lockType, () -> { + run.run(); + return null; + }, catchby == null ? null : e -> { + catchby.accept(e); + return null; + }, finaby == null ? null : _ -> { + finaby.run(); + return null; + }); + } - //----------------------------------------------- + /** + * 使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,遇到异常不处理返回{@code null} + * + * @param lockType 锁类型 + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常返回{@code null} + * + * @see trySupplier + * @see LockType + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + R lock(@NotNull LockType lockType, @NotNull trySupplier run) + { return lock(lockType, run, null, null); } - /** - * 使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,遇到异常不处理返回{@code null} - * - * @param lockType 锁类型 - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常返回{@code null} - * - * @see trySupplier - * @see LockType - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - R lock(@NotNull LockType lockType, @NotNull trySupplier run) { return lock(lockType, run, null, null); } + /** + * 使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,遇到异常不处理 + * + * @param lockType 锁类型 + * @param run 执行的回调 + * + * @see tryRunnable + * @see LockType + * @see #lock(LockType, tryRunnable, Consumer, Runnable) + * @since 0.1.0 + */ + default + void lock(@NotNull LockType lockType, @NotNull tryRunnable run) + { lock(lockType, run, null, null); } - /** - * 使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-catch-finally块,遇到异常不处理 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * - * @see tryRunnable - * @see LockType - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void lock(@NotNull LockType lockType, @NotNull tryRunnable run) { - lock(lockType, () -> { - run.run(); - return null; - }, null, null); - } + /** + * 尝试使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param lockType 锁类型 + * @param run 带返回的回调 + * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @implSpec 如果有传入 {@code finaby} 回调则返回值由{@code finaby}主导,传入{@code finaby}的值根据是否发生异常传入{@code run}的返回值或{@code null}
+ * 任意一个回调为空时直接穿透,使用上一个正确执行的值进行传递或者返回,发生异常会执行{@code finaby}但是不会返回内容 + * @see trySupplier + * @see Function + * @see LockType + * @since 0.1.0 + */ + R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception; - //----------------------------------------------- + /** + * 尝试使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param lockType 锁类型 + * @param run 执行的回调 + * @param finaby 进入finally块后的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see tryRunnable + * @see Runnable + * @see LockType + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + default + void trylock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception { + trylock(lockType, () -> { + run.run(); + return null; + }, finaby == null ? null : _ -> { + finaby.run(); + return null; + }); + } - /** - * 尝试使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
- * 所有回调均在并发域内执行 - * - * @param lockType 锁类型 - * @param run 带返回的回调 - * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @implSpec 如果有传入 {@code finaby} 回调则返回值由{@code finaby}主导,传入{@code finaby}的值根据是否发生异常传入{@code run}的返回值或{@code null}
- * 任意一个回调为空时直接穿透,使用上一个正确执行的值进行传递或者返回,发生异常会执行{@code finaby}但是不会返回内容 - * @see trySupplier - * @see Function - * @see LockType - * @since 0.1.0 - */ - R trylock(@NotNull LockType lockType, @NotNull trySupplier run, @Nullable Function finaby) throws Exception; + /** + * 尝试使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,遇到异常会抛出 + * + * @param lockType 锁类型 + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + default + R trylock(@NotNull LockType lockType, @NotNull trySupplier run) throws Exception + { return trylock(lockType, run, null); } - /** - * 尝试使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
- * 所有回调均在并发域内执行 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * @param finaby 进入finally块后的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see tryRunnable - * @see Runnable - * @see LockType - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - void trylock(@NotNull LockType lockType, @NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception { - trylock(lockType, () -> { - run.run(); - return null; - }, finaby == null ? null : _ -> { - finaby.run(); - return null; - }); - } + /** + * 尝试使用锁执行指定回调 + *

+ * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,遇到异常会抛出 + * + * @param lockType 锁类型 + * @param run 执行的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see tryRunnable + * @see LockType + * @see #trylock(LockType, tryRunnable, Runnable) + * @since 0.1.0 + */ + default + void trylock(@NotNull LockType lockType, @NotNull tryRunnable run) throws Exception + { trylock(lockType, run, null); } - //----------------------------------------------- + /** + * 使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-catch-finally块,遇到异常不处理 + * + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容 + * + * @see trySupplier + * @see LockType#READ + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + R read(@NotNull trySupplier run) + { return lock(LockType.READ, run, null, null); } - /** - * 尝试使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,遇到异常会抛出 - * - * @param lockType 锁类型 - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see trySupplier - * @see LockType - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - R trylock(@NotNull LockType lockType, @NotNull trySupplier run) throws Exception - { return trylock(lockType, run, null); } + /** + * 使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-catch-finally块,遇到异常不处理 + * + * @param run 执行的回调 + * + * @see tryRunnable + * @see LockType#READ + * @see #lock(LockType, tryRunnable, Consumer, Runnable) + * @since 0.1.0 + */ + default + void read(@NotNull tryRunnable run) + { lock(LockType.READ, run, null, null); } - /** - * 尝试使用锁执行指定回调 - *

- * 可通过传入{@link LockType}指定锁的类型,运行时自带try-finally块,遇到异常会抛出 - * - * @param lockType 锁类型 - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see tryRunnable - * @see LockType - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - void trylock(@NotNull LockType lockType, @NotNull tryRunnable run) throws Exception { - trylock(lockType, () -> { - run.run(); - return null; - }, null); - } + /** + * 使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-catch-finally块,遇到异常不处理 + * + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容 + * + * @see trySupplier + * @see LockType#WRITE + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + R write(@NotNull trySupplier run) + { return lock(LockType.WRITE, run, null, null); } - /*--------------------------------------------------------------------------------------------*/ + /** + * 使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-catch-finally块,遇到异常不处理 + * + * @param run 执行的回调 + * + * @see tryRunnable + * @see LockType#WRITE + * @see #lock(LockType, tryRunnable, Consumer, Runnable) + * @since 0.1.0 + */ + default + void write(@NotNull tryRunnable run) + { lock(LockType.WRITE, run, null, null); } - /** - * 使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-catch-finally块,遇到异常不处理 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @see trySupplier - * @see LockType#READ - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - R read(@NotNull trySupplier run) { return lock(LockType.READ, run, null, null); } + /** + * 使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param run 带返回的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调,传入前两个回调的返回值 + * @param 要返回的数据类型 + * + * @return 回调返回的内容 + * + * @see trySupplier + * @see Function + * @see LockType#READ + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + R read(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) + { return lock(LockType.READ, run, catchby, finaby); } - /** - * 使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-catch-finally块,遇到异常不处理 - * - * @param run 执行的回调 - * - * @see tryRunnable - * @see LockType#READ - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void read(@NotNull tryRunnable run) { - lock(LockType.READ, () -> { - run.run(); - return null; - }, null, null); - } + /** + * 使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param run 执行的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调 + * + * @see tryRunnable + * @see Consumer + * @see Runnable + * @see LockType#READ + * @see #lock(LockType, tryRunnable, Consumer, Runnable) + * @since 0.1.0 + */ + default + void read(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { lock(LockType.READ, run, catchby, finaby); } - /** - * 使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-catch-finally块,遇到异常不处理 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @see trySupplier - * @see LockType#WRITE - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - R write(@NotNull trySupplier run) { return lock(LockType.WRITE, run, null, null); } + /** + * 使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param run 带返回的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调,传入前两个回调的返回值 + * @param 要返回的数据类型 + * + * @return 回调返回的内容 + * + * @see trySupplier + * @see Function + * @see LockType#WRITE + * @see #lock(LockType, trySupplier, Function, Function) + * @since 0.1.0 + */ + default + R write(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) + { return lock(LockType.WRITE, run, catchby, finaby); } - /** - * 使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-catch-finally块,遇到异常不处理 - * - * @param run 执行的回调 - * - * @see tryRunnable - * @see LockType#WRITE - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void write(@NotNull tryRunnable run) { - lock(LockType.WRITE, () -> { - run.run(); - return null; - }, null, null); - } + /** + * 使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
+ * 所有回调均在并发域内执行 + * + * @param run 执行的回调 + * @param catchby 进入catch块后的回调,传入当前异常 + * @param finaby 进入finally块后的回调 + * + * @see tryRunnable + * @see Consumer + * @see Runnable + * @see LockType#WRITE + * @see #lock(LockType, tryRunnable, Consumer, Runnable) + * @since 0.1.0 + */ + default + void write(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) + { lock(LockType.WRITE, run, catchby, finaby); } - //----------------------------------------------- + /** + * 尝试使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-finally块,遇到异常会抛出 + * + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#READ + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + default + R tryread(@NotNull trySupplier run) throws Exception + { return trylock(LockType.READ, run, null); } - /** - * 使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param run 带返回的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @see trySupplier - * @see Function - * @see LockType#READ - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - R read(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) - { return lock(LockType.READ, run, catchby, finaby); } + /** + * 尝试使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-finally块,遇到异常会抛出 + * + * @param run 执行的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see tryRunnable + * @see LockType#READ + * @see #trylock(LockType, tryRunnable, Runnable) + * @since 0.1.0 + */ + default + void tryread(@NotNull tryRunnable run) throws Exception + { trylock(LockType.READ, run, null); } - /** - * 使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - * - * @see tryRunnable - * @see Consumer - * @see Runnable - * @see LockType#READ - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void read(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) { - lock(LockType.READ, () -> { - run.run(); - return null; - }, catchby == null ? null : e -> { - catchby.accept(e); - return null; - }, finaby == null ? null : _ -> { - finaby.run(); - return null; - }); - } + /** + * 尝试使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-finally块,遇到异常会抛出 + * + * @param run 带返回的回调 + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#WRITE + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + default + R trywrite(@NotNull trySupplier run) throws Exception + { return trylock(LockType.WRITE, run, null); } - /** - * 使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param run 带返回的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调,传入前两个回调的返回值 - * @param 要返回的数据类型 - * - * @return 回调返回的内容 - * - * @see trySupplier - * @see Function - * @see LockType#WRITE - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - R write(@NotNull trySupplier run, @Nullable Function catchby, @Nullable Function finaby) - { return lock(LockType.WRITE, run, catchby, finaby); } + /** + * 尝试使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-finally块,遇到异常会抛出 + * + * @param run 执行的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see tryRunnable + * @see LockType#WRITE + * @see #trylock(LockType, tryRunnable, Runnable) + * @since 0.1.0 + */ + default + void trywrite(@NotNull tryRunnable run) throws Exception + { trylock(LockType.WRITE, run, null); } - /** - * 使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-catch-finally块,通过三个回调参数插入不同的块中执行
- * 所有回调均在并发域内执行 - * - * @param run 执行的回调 - * @param catchby 进入catch块后的回调,传入当前异常 - * @param finaby 进入finally块后的回调 - * - * @see tryRunnable - * @see Consumer - * @see Runnable - * @see LockType#WRITE - * @see #lock(LockType, trySupplier, Function, Function) - * @since 0.1.0 - */ - default - void write(@NotNull tryRunnable run, @Nullable Consumer catchby, @Nullable Runnable finaby) { - lock(LockType.WRITE, () -> { - run.run(); - return null; - }, catchby == null ? null : e -> { - catchby.accept(e); - return null; - }, finaby == null ? null : _ -> { - finaby.run(); - return null; - }); - } + /** + * 尝试使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param run 带返回的回调 + * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#READ + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.0 + */ + default + R tryread(@NotNull trySupplier run, @Nullable Function finaby) throws Exception + { return trylock(LockType.READ, run, finaby); } - //----------------------------------------------- + /** + * 尝试使用读锁执行指定回调 + *

+ * 调用读锁执行,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param run 执行的回调 + * @param finaby 进入finally块后的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#READ + * @see #trylock(LockType, tryRunnable, Runnable) + * @since 0.1.1 + */ + default + void tryread(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception + { trylock(LockType.READ, run, finaby); } - /** - * 尝试使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-finally块,遇到异常会抛出 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see trySupplier - * @see LockType#READ - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - R tryread(@NotNull trySupplier run) throws Exception { return trylock(LockType.READ, run, null); } + /** + * 尝试使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param run 带返回的回调 + * @param finaby 进入finally块后的回调,传入前一个回调的返回值,遇到异常传入{@code null} + * @param 要返回的数据类型 + * + * @return 回调返回的内容,遇到异常不返回 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#WRITE + * @see #trylock(LockType, trySupplier, Function) + * @since 0.1.1 + */ + default + R trywrite(@NotNull trySupplier run, @Nullable Function finaby) throws Exception + { return trylock(LockType.WRITE, run, finaby); } - /** - * 尝试使用读锁执行指定回调 - *

- * 调用读锁执行,运行时自带try-finally块,遇到异常会抛出 - * - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see tryRunnable - * @see LockType#READ - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - void tryread(@NotNull tryRunnable run) throws Exception { - trylock(LockType.READ, () -> { - run.run(); - return null; - }, null); - } + /** + * 尝试使用写锁执行指定回调 + *

+ * 调用写锁执行,运行时自带try-finally块,通过两个回调参数插入不同的块中执行,遇到异常会抛出
+ * 所有回调均在并发域内执行 + * + * @param run 执行的回调 + * @param finaby 进入finally块后的回调 + * + * @throws Exception 异常类型根据实际运行时回调抛出决定 + * @see trySupplier + * @see LockType#WRITE + * @see #trylock(LockType, tryRunnable, Runnable) + * @since 0.1.1 + */ + default + void trywrite(@NotNull tryRunnable run, @Nullable Runnable finaby) throws Exception + { trylock(LockType.WRITE, run, finaby); } - /** - * 尝试使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-finally块,遇到异常会抛出 - * - * @param run 带返回的回调 - * @param 要返回的数据类型 - * - * @return 回调返回的内容,遇到异常不返回 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see trySupplier - * @see LockType#WRITE - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - R trywrite(@NotNull trySupplier run) throws Exception { return trylock(LockType.WRITE, run, null); } + /** + * 获取传统并发实现 + * + * @return 获取的并发控制对象 + * + * @see ObjLock + */ + @NotNull + static + ObjLock newObjLock() { return new ObjLock(); } - /** - * 尝试使用写锁执行指定回调 - *

- * 调用写锁执行,运行时自带try-finally块,遇到异常会抛出 - * - * @param run 执行的回调 - * - * @throws Exception 异常类型根据实际运行时回调抛出决定 - * @see tryRunnable - * @see LockType#WRITE - * @see #trylock(LockType, trySupplier, Function) - * @since 0.1.0 - */ - default - void trywrite(@NotNull tryRunnable run) throws Exception { - trylock(LockType.WRITE, () -> { - run.run(); - return null; - }, null); - } + /** + * 获取可重入锁实现 + * + * @return 获取的并发控制对象 + * + * @see ReLock + */ + @NotNull + static + ReLock newReLock() { return new ReLock(); } - /*--------------------------------------------------------------------------------------------*/ - - /** - * 获取传统并发实现 - * - * @return 获取的并发控制对象 - * - * @see ObjLock - */ - @NotNull - static - ObjLock newObjLock() { return new ObjLock(); } - - /** - * 获取可重入锁实现 - * - * @return 获取的并发控制对象 - * - * @see ReLock - */ - @NotNull - static - ReLock newReLock() { return new ReLock(); } - - /** - * 获取读写锁实现 - * - * @return 获取的并发控制对象 - * - * @see RWLock - */ - @NotNull - static - RWLock newRWLock() { return new RWLock(); } + /** + * 获取读写锁实现 + * + * @return 获取的并发控制对象 + * + * @see RWLock + */ + @NotNull + static + RWLock newRWLock() { return new RWLock(); } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/e/LockType.java b/src/main/java/fybug/nulll/pdconcurrent/e/LockType.java index 0d722ea273db811863a73af8443091d55bac7a1b..0562106c5445f0c453580b1a11a0b88c7e0d834c 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/e/LockType.java +++ b/src/main/java/fybug/nulll/pdconcurrent/e/LockType.java @@ -9,10 +9,10 @@ package fybug.nulll.pdconcurrent.e; */ public enum LockType { - /** 读锁 */ - READ, - /** 写锁 */ - WRITE, - /** 不上锁 */ - NOLOCK + /** 读锁 */ + READ, + /** 写锁 */ + WRITE, + /** 不上锁 */ + NOLOCK } diff --git a/src/main/java/fybug/nulll/pdconcurrent/fun/tryConsumer.java b/src/main/java/fybug/nulll/pdconcurrent/fun/tryConsumer.java index c02de60f1fa1c24c5a7d4ab7b86797c0ba5f25fb..b1a3114481f9779f345170caacf7d816a17f2eaf 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/fun/tryConsumer.java +++ b/src/main/java/fybug/nulll/pdconcurrent/fun/tryConsumer.java @@ -14,35 +14,35 @@ import jakarta.validation.constraints.NotNull; */ public interface tryConsumer { - /** @see Consumer#accept(Object) */ - void accept(T t) throws Exception; + /** @see Consumer#accept(Object) */ + void accept(T t) throws Exception; - /** @since 0.0.2 */ - @NotNull - default - tryConsumer andThen(@Nullable tryRunnable after) { - if ( after != null ) { - return t -> { - accept(t); - after.run(); - }; - } - return this; - } + /** @since 0.0.2 */ + @NotNull + default + tryConsumer andThen(@Nullable tryRunnable after) { + if ( after != null ) { + return t -> { + accept(t); + after.run(); + }; + } + return this; + } - /** @since 0.0.2 */ - @NotNull - default - tryFunction andThen(@Nullable trySupplier after) { - if ( after != null ) { - return t -> { - accept(t); - return after.get(); - }; - } - return t -> { - accept(t); - return null; - }; - } + /** @since 0.0.2 */ + @NotNull + default + tryFunction andThen(@Nullable trySupplier after) { + if ( after != null ) { + return t -> { + accept(t); + return after.get(); + }; + } + return t -> { + accept(t); + return null; + }; + } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/fun/tryFunction.java b/src/main/java/fybug/nulll/pdconcurrent/fun/tryFunction.java index aeeed13cea8bbe54cf13e720d6050037c5deacba..d3fa122e489991034d55560ae3175c1ec31c9fcf 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/fun/tryFunction.java +++ b/src/main/java/fybug/nulll/pdconcurrent/fun/tryFunction.java @@ -15,24 +15,24 @@ import jakarta.validation.constraints.NotNull; */ public interface tryFunction { - /** @see Function#apply(Object) */ - R apply(T t) throws Exception; + /** @see Function#apply(Object) */ + R apply(T t) throws Exception; - /** @since 0.0.2 */ - @NotNull - default - tryConsumer andThen(@Nullable tryConsumer after) { - if ( after != null ) - return t -> after.accept(apply(t)); - return this::apply; - } + /** @since 0.0.2 */ + @NotNull + default + tryConsumer andThen(@Nullable tryConsumer after) { + if ( after != null ) + return t -> after.accept(apply(t)); + return this::apply; + } - /** @since 0.0.2 */ - @NotNull - default - tryFunction andThen(@Nullable tryFunction after) { - if ( after != null ) - return t -> after.apply(apply(t)); - return (tryFunction) this; - } + /** @since 0.0.2 */ + @NotNull + default + tryFunction andThen(@Nullable tryFunction after) { + if ( after != null ) + return t -> after.apply(apply(t)); + return (tryFunction) this; + } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/fun/tryRunnable.java b/src/main/java/fybug/nulll/pdconcurrent/fun/tryRunnable.java index 970eeacf5601ed361df08aaee1baf8d42b88b7aa..99be4362ec66add63659e87796c830002afdffbb 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/fun/tryRunnable.java +++ b/src/main/java/fybug/nulll/pdconcurrent/fun/tryRunnable.java @@ -11,64 +11,64 @@ import jakarta.validation.constraints.NotNull; @FunctionalInterface public interface tryRunnable { - /** @see Runnable#run() */ - void run() throws Exception; + /** @see Runnable#run() */ + void run() throws Exception; - /** @since 0.0.2 */ - @NotNull - default - tryRunnable andThen(@Nullable tryRunnable after) { - if ( after != null ) { - return () -> { - run(); - after.run(); - }; - } - return this; - } + /** @since 0.0.2 */ + @NotNull + default + tryRunnable andThen(@Nullable tryRunnable after) { + if ( after != null ) { + return () -> { + run(); + after.run(); + }; + } + return this; + } - /** @since 0.0.2 */ - @NotNull - default - trySupplier andThen(@Nullable trySupplier after) { - if ( after != null ) { - return () -> { - run(); - return after.get(); - }; - } - return () -> { - run(); - return null; - }; - } + /** @since 0.0.2 */ + @NotNull + default + trySupplier andThen(@Nullable trySupplier after) { + if ( after != null ) { + return () -> { + run(); + return after.get(); + }; + } + return () -> { + run(); + return null; + }; + } - /** @since 0.0.2 */ - @NotNull - default - tryRunnable andThen(@Nullable tryConsumer after) { - if ( after != null ) { - return () -> { - run(); - after.accept(null); - }; - } - return this; - } + /** @since 0.0.2 */ + @NotNull + default + tryRunnable andThen(@Nullable tryConsumer after) { + if ( after != null ) { + return () -> { + run(); + after.accept(null); + }; + } + return this; + } - /** @since 0.0.2 */ - @NotNull - default - trySupplier andThen(@Nullable tryFunction after) { - if ( after != null ) { - return () -> { - run(); - return after.apply(null); - }; - } - return () -> { - run(); - return null; - }; - } + /** @since 0.0.2 */ + @NotNull + default + trySupplier andThen(@Nullable tryFunction after) { + if ( after != null ) { + return () -> { + run(); + return after.apply(null); + }; + } + return () -> { + run(); + return null; + }; + } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/fun/trySupplier.java b/src/main/java/fybug/nulll/pdconcurrent/fun/trySupplier.java index d1b8ebfe086d9b3cdc1f9566bc2e74a13c067716..5acbc79550cc20962d9377a7388708cd92b1a71f 100644 --- a/src/main/java/fybug/nulll/pdconcurrent/fun/trySupplier.java +++ b/src/main/java/fybug/nulll/pdconcurrent/fun/trySupplier.java @@ -15,24 +15,24 @@ import jakarta.validation.constraints.NotNull; @FunctionalInterface public interface trySupplier { - /** @see Supplier#get() */ - R get() throws Exception; + /** @see Supplier#get() */ + R get() throws Exception; - /** @since 0.0.2 */ - @NotNull - default - tryRunnable andThen(@Nullable tryConsumer after) { - if ( after != null ) - return () -> after.accept(get()); - return this::get; - } + /** @since 0.0.2 */ + @NotNull + default + tryRunnable andThen(@Nullable tryConsumer after) { + if ( after != null ) + return () -> after.accept(get()); + return this::get; + } - /** @since 0.0.2 */ - @NotNull - default - trySupplier andThen(@Nullable tryFunction after) { - if ( after != null ) - return () -> after.apply(get()); - return (trySupplier) this; - } + /** @since 0.0.2 */ + @NotNull + default + trySupplier andThen(@Nullable tryFunction after) { + if ( after != null ) + return () -> after.apply(get()); + return (trySupplier) this; + } } diff --git a/src/main/java/fybug/nulll/pdconcurrent/package-info.java b/src/main/java/fybug/nulll/pdconcurrent/package-info.java index 640060ae19b852a6dc78aafd430b0b72bef8dd31..b3e8496e22213573e910b257d77636e4dd9b46a3 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.0 + * @version 0.1.1 * @since JDK 23+ */ package fybug.nulll.pdconcurrent; \ No newline at end of file diff --git a/src/test/java/a.java b/src/test/java/a.java deleted file mode 100644 index d82921a30541930c9de80a504b855e81e2db02f4..0000000000000000000000000000000000000000 --- a/src/test/java/a.java +++ /dev/null @@ -1,29 +0,0 @@ -public -class a { - public static - void main(String[] args) { - try { - System.out.println(a()); - } catch ( Exception e ) { - System.out.println("11111"); - } - } - - private static - String a() throws Exception { - String a; - try { - a = "2"; - b(); - } finally { - a = "3"; - System.out.println("11111"); - } - return a; - } - - private static - void b() throws Exception { - throw new Exception(); - } -}