pool:
- ObjectPool 移除 @Deprecated("未实现") 标记,完成实现
* close() 正确设置关闭标志、销毁空闲对象并 signalAll 唤醒等待者
* borrow() 修复竞态:循环重取避免 poll 返回 null;创建失败回滚计数
* 区分「无限等待 / 立即超时 / 有限等待」三种超时语义
* returnObject() 拒绝外来对象或已关闭状态,直接销毁以防污染
* 空闲队列由 ConcurrentLinkedQueue 改为 ArrayDeque,所有访问统一持锁,
移除冗余的 AtomicInteger;idleSize / activeSize / totalSize 返回精确值
* 新增 snapshot() 提供 idle/active/total/closed 一致快照
- 新增 Pool.kt:抽取通用接口,use() 使用 try/finally 保证归还,
新增 useResult() 支持带返回值场景
- 新增 ObjectPoolData.kt:池对象元数据包装,提供 release() 语法糖
- 新增 ThreadPool.kt:可复用线程池
* 池对象为 Worker(长期存活的守护线程 + LinkedBlockingQueue 任务队列),
线程通过循环消费任务实现真正复用,保留 ThreadLocal 状态
* 提供 execute / call / submit / submitRunnable 四种任务投递方式
* submit 返回 CompletableFuture,任务完成后自动归还 Worker
* 内置 submitted/completed/failed 计数与 uncaughtHandler 兜底,
任务异常不会导致 Worker 线程死亡
* 提供 defaultPool / fixed / single 静态工厂与 closeAndAwait 优雅关闭
json:
- Jackson3JsonApi / JacksonJsonApi 暴露 getObjectMapper() 便于外部扩展
metadata:
- MingliUtilsMetaData 改为 data class,获得 equals/hashCode/copy/toString
mybatisplus:
- 修正 BaseMapperQuery @Deprecated 的 ReplaceWith 表达式(去掉多余的 <*>)
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2026 mingliqiye
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ProjectName mingli-utils
|
||||
* ModuleName mingli-utils.main
|
||||
* CurrentFile Pool.kt
|
||||
* LastUpdate 2026-10-12 13:43:53
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.pool
|
||||
|
||||
/**
|
||||
* 对象池通用接口。
|
||||
*/
|
||||
interface Pool<T> {
|
||||
/** 关闭池并释放全部资源 */
|
||||
fun close()
|
||||
|
||||
/** 借出一个对象,[timeoutMillis] <= 0 表示无限等待 */
|
||||
fun borrow(timeoutMillis: Long): ObjectPoolData<T>
|
||||
|
||||
/** 无限等待地借出一个对象 */
|
||||
fun borrow(): ObjectPoolData<T> = borrow(0)
|
||||
|
||||
/** 归还对象 */
|
||||
fun returnObject(obj: ObjectPoolData<T>)
|
||||
|
||||
/** 使用默认超时执行 [funs] */
|
||||
fun use(funs: (ObjectPoolData<T>) -> Unit) = use(0, funs)
|
||||
|
||||
/** 创建一个新的池对象 */
|
||||
fun init(id: Int): ObjectPoolData<T>
|
||||
|
||||
/** 销毁池对象 */
|
||||
fun destroy(data: ObjectPoolData<T>)
|
||||
|
||||
/**
|
||||
* 借出对象执行 [funs],无论是否抛出异常都会自动归还。
|
||||
*/
|
||||
fun use(timeoutMillis: Long, funs: (ObjectPoolData<T>) -> Unit) {
|
||||
val data = borrow(timeoutMillis)
|
||||
try {
|
||||
funs(data)
|
||||
} finally {
|
||||
runCatching { returnObject(data) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 借出对象执行 [funs] 并返回其结果,无论是否抛出异常都会自动归还。
|
||||
*/
|
||||
fun <R> useResult(timeoutMillis: Long = 0, funs: (ObjectPoolData<T>) -> R): R {
|
||||
val data = borrow(timeoutMillis)
|
||||
try {
|
||||
return funs(data)
|
||||
} finally {
|
||||
runCatching { returnObject(data) }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user