no message

This commit is contained in:
2026-02-05 11:42:52 +08:00
parent f2ee83ca64
commit a5a532bc82
90 changed files with 7945 additions and 2458 deletions
@@ -0,0 +1,112 @@
/*
* 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 ThreadEvent.kt
* LastUpdate 2026-02-05 11:20:59
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.thread
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* 线程事件同步工具类,用于线程间的事件通知和等待
*/
open class ThreadEvent<T> {
private var latch = CountDownLatch(1)
@Volatile
private var data: T
constructor(data: T) {
this.data = data
}
/**
* 获取当前事件状态
*
* @return 当前事件的数据值,如果未设置则返回null
*/
fun get(): T = data
/**
* 设置事件数据并释放等待的线程
*
* @param data 要设置的事件数据
*/
@Synchronized
fun set(data: T) {
this.data = data
latch.countDown()
}
/**
* 重置事件状态,重新初始化CountDownLatch
*/
@Synchronized
fun reset() {
latch = CountDownLatch(1)
}
/**
* 等待事件被设置,阻塞当前线程直到事件被触发
*/
fun await() {
latch.await()
}
/**
* 等待事件被设置并获取数据
*
* @return 事件数据,当事件被设置后返回对应的数据
*/
fun awaitAndGet(): T {
await()
return data
}
/**
* 在指定超时时间内等待事件被设置并获取数据
*
* @param timeout 等待超时时间
* @param unit 时间单位
* @return 事件数据,如果在超时时间内事件被设置则返回数据,否则返回null
*/
fun awaitAndGet(timeout: Long, unit: TimeUnit): T? {
await(timeout, unit)
return data
}
/**
* 在指定超时时间内等待事件被设置
*
* @param timeout 等待超时时间
* @param unit 时间单位
* @return 如果在超时时间内事件被设置则返回true,否则返回false
*/
fun await(timeout: Long, unit: TimeUnit): Boolean {
return latch.await(timeout, unit)
}
fun setAndReset(data: T) {
set(data)
reset()
}
}
@@ -0,0 +1,110 @@
/*
* 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 ThreadRunner.kt
* LastUpdate 2026-02-05 11:20:59
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.thread
import com.mingliqiye.utils.netty.NamedThreadFactory
import com.mingliqiye.utils.system.availableProcessors
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
/**
* 线程执行器工具类,用于管理线程池并提供异步任务执行功能
*/
object ThreadRunner {
var executorService: ExecutorService? = null
/**
* 关闭线程池执行器
* 首先尝试正常关闭,如果失败则强制关闭
*/
@JvmStatic
fun close() {
try {
executorService?.shutdown()
} catch (_: Exception) {
executorService?.shutdownNow()
}
}
/**
* 初始化线程池执行器
* 创建固定大小的线程池,线程数量为可用处理器核心数的两倍
*
* @param string 线程名称前缀,默认为"MingliUtilThread"
*/
@JvmStatic
fun init(string: String = "MingliUtilThread") {
executorService = Executors.newFixedThreadPool(
availableProcessors * 2,
NamedThreadFactory { clazz, poolNumber, threadNumber ->
"$string #$threadNumber"
})
}
/**
* 异步执行Runnable任务
*
* @param runnable 要执行的Runnable任务
* @return Future对象,可用于获取任务执行结果或控制任务状态
*/
@JvmStatic
fun runThread(runnable: Runnable): Future<*> {
return executorService!!.submit(runnable)
}
/**
* 异步执行Callable任务
*
* @param runnable 要执行的Callable任务
* @return Future对象,可用于获取任务执行结果或控制任务状态
*/
@JvmStatic
fun <T> runThread(runnable: Callable<T>): Future<T> {
return executorService!!.submit(runnable)
}
/**
* 同步执行Runnable任务,阻塞等待任务完成
*
* @param runnable 要执行的Runnable任务
* @return 任务执行结果(通常为null)
*/
@JvmStatic
fun runThreadAwait(runnable: Runnable): Any {
return executorService!!.submit(runnable).get()
}
/**
* 同步执行Callable任务,阻塞等待任务完成并返回结果
*
* @param runnable 要执行的Callable任务
* @return 任务执行结果
*/
@JvmStatic
fun <T> runThreadAwait(runnable: Callable<T>): T {
return executorService!!.submit(runnable).get()
}
}
@@ -0,0 +1,43 @@
/*
* 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 ThreadUtils.kt
* LastUpdate 2026-01-31 20:48:19
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.thread
/**
* 线程工具类,提供线程名称的获取和设置功能
*/
object ThreadUtils {
/**
* 获取或设置当前线程的名称
*
* Getter: 返回当前线程的名称
* Setter: 设置当前线程的名称
*
* @return 当前线程的名称字符串
*/
@JvmStatic
var name: String
get() = Thread.currentThread().name
set(s) {
Thread.currentThread().name = s
}
}