package com.quyunshuo.module.home.utils

import java.util.concurrent.atomic.AtomicBoolean

class SingleShotLock {
    private val isLocked = AtomicBoolean(false)

    // 尝试执行，成功返回true并自动加锁，失败返回false
    fun tryExecute(action: () -> Unit): Boolean {
        if (isLocked.compareAndSet(false, true)) {
            action.invoke()
            return true
        }
        return false
    }

    // 手动解锁
    fun unlock() {
        isLocked.set(false)
    }
}