feat(thread): 新增线程工具集与线程池构建体系
- ThreadPoolBuilder: DSL 风格线程池构建器,含 fixed/single/cached/io/cpu/scheduled 预设 - GlobalThreadPool: 全局 IO/CPU/定时三类共享线程池及便捷提交函数 - ThreadPoolMonitor: 线程池运行快照与周期监控 - ThreadPoolExt: ExecutorService/Future/CompletableFuture 便捷扩展 - AsyncTasks: 并行执行、竞速执行、重试与定时任务编排 - ThreadLocalDelegates: ThreadLocal 属性委托 - ThreadExt: Thread/Runnable 扩展与守护线程便捷创建 - ThreadUtils 增强: 休眠、线程枚举、全线程栈转储 - ThreadEvent 增强: operator invoke、属性委托、isSet 与超时取数 - 移除 pool 包旧 ThreadPool.kt - 新增 5 个测试类共 83 个单元测试
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 ThreadLocalDelegates.kt
|
||||
* LastUpdate 2026-10-19 20:51:35
|
||||
* UpdateUser MingLiPro
|
||||
*/
|
||||
|
||||
package com.mingliqiye.utils.thread
|
||||
|
||||
import java.util.function.Supplier
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
/**
|
||||
* 将 [ThreadLocal] 包装为 Kotlin 属性委托,通过 `by` 关键字即可实现线程级变量:
|
||||
*
|
||||
* ```kotlin
|
||||
* object RequestContext {
|
||||
* var userId: Long by threadLocal { 0L }
|
||||
* var traceId: String? by nullableThreadLocal()
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* 每个线程读写互不影响,切换线程后自动获得新线程的隔离值。
|
||||
*/
|
||||
class ThreadLocalDelegate<T>(
|
||||
private val threadLocal: ThreadLocal<T>,
|
||||
) : ReadWriteProperty<Any?, T> {
|
||||
|
||||
override fun getValue(thisRef: Any?, property: KProperty<*>): T = threadLocal.get()
|
||||
|
||||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
||||
threadLocal.set(value)
|
||||
}
|
||||
|
||||
override fun toString(): String = threadLocal.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建带初始值的线程局部属性委托
|
||||
*
|
||||
* @param initial 每个线程首次访问时的初始化器
|
||||
*/
|
||||
fun <T> threadLocal(initial: () -> T): ThreadLocalDelegate<T> =
|
||||
ThreadLocalDelegate(ThreadLocal.withInitial(Supplier(initial)))
|
||||
|
||||
/**
|
||||
* 创建无初始值的线程局部属性委托,属性类型需声明为可空:
|
||||
*
|
||||
* ```kotlin
|
||||
* var value: String? by nullableThreadLocal()
|
||||
* ```
|
||||
*/
|
||||
fun <T : Any> nullableThreadLocal(): ThreadLocalDelegate<T?> =
|
||||
ThreadLocalDelegate(ThreadLocal())
|
||||
|
||||
/**
|
||||
* 将已有 [ThreadLocal] 实例包装为属性委托
|
||||
*/
|
||||
fun <T> ThreadLocal<T>.asDelegate(): ThreadLocalDelegate<T> = ThreadLocalDelegate(this)
|
||||
Reference in New Issue
Block a user