Files
mingli-utils/src/main/kotlin/com/mingliqiye/utils/concurrent/IsChanged.kt
T
minglipro 51eb838a36
Gitea Actions Build / Build (push) Canceled after 0s
docs: 补全 src 全部 Kotlin KDoc 与 Java Javadoc,版本升级至 5.5.1
为所有缺失文档的公开声明补充中文 KDoc/Javadoc(含 exception、json、
springboot、tuple 生成类、MediaType 枚举项及测试),仅插入注释不改代码;
gradle.properties VERSIONS 5.4.4 -> 5.5.1。
2026-11-17 21:07:58 +08:00

105 lines
2.7 KiB
Kotlin

/*
* 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 IsChanged.kt
* LastUpdate 2026-01-31 21:13:45
* UpdateUser MingLiPro
*/
package com.mingliqiye.utils.concurrent
import java.util.*
import java.util.concurrent.atomic.AtomicReference
/**
* IsChanged 类提供了一个线程安全的包装器,用于检测值是否发生变化。
* 它基于 AtomicReference 实现,适用于需要监控数据变更的并发场景。
*
* @param T 泛型类型,表示被包装的数据类型
* @author MingLiPro
*/
class IsChanged<T> {
/**
* 使用 AtomicReference 来保证对数据的原子操作
*/
private val atomicReferenceData: AtomicReference<T> = AtomicReference()
/**
* 无参构造函数,初始数据为 null
*/
constructor()
/**
* 带参数的构造函数,使用指定的初始值初始化
*
* @param data 初始数据值
*/
constructor(data: T) : super() {
atomicReferenceData.set(data)
}
/**
* 设置新的数据值,不检查是否发生变化
*
* @param data 要设置的新数据值
*/
fun set(data: T) {
atomicReferenceData.set(data)
}
/**
* 获取当前数据值
*
* @return 当前数据值
*/
fun get(): T {
return atomicReferenceData.get()
}
/**
* 设置新的数据值并返回旧的数据值
*
* @param data 要设置的新数据值
* @return 设置前的旧数据值
*/
fun setAndGet(data: T): T {
return atomicReferenceData.getAndSet(data)
}
/**
* 设置新的数据值,如果新值与当前值不同则更新并返回 true,否则返回 false
* 使用 CAS(Compare-And-Swap) 操作确保线程安全
*
* @param data 要设置的新数据值
* @return 如果值发生变化返回 true,否则返回 false
*/
fun setAndChanged(data: T): Boolean {
var currentData: T
do {
currentData = get()
// 如果新值与当前值相等,则认为没有变化,直接返回 false
if (Objects.equals(data, currentData)) {
return false
}
// 使用 CAS 操作尝试更新值,如果失败则重试
} while (!atomicReferenceData.compareAndSet(currentData, data))
// 成功更新值,返回 true 表示发生了变化
return true
}
}