All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.aqiu202.lock.base.LockValueHolder Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.github.aqiu202.lock.base;

import java.util.Objects;

/**
 * 
LockValueHolder
* * @author aqiu 2020/12/2 15:14 **/ public abstract class LockValueHolder { private static final ThreadLocal threadLocal = new ThreadLocal<>(); private static final ThreadLocal inheritableThreadLocal = new InheritableThreadLocal<>(); static LockValueHolderStrategy strategy = LockValueHolderStrategy.thread; public static String setIfAbsent(String value) { String old = getValue(); if (old == null) { setValue(value); return value; } return old; } public static void setValue(String value) { getHolder().set(value); } public static String getValue() { return getHolder().get(); } public static boolean hasValue() { return Objects.nonNull(getValue()); } public static void remove() { getHolder().remove(); } private static ThreadLocal getHolder() { if (strategy == LockValueHolderStrategy.inheritable_thread) { return inheritableThreadLocal; } return threadLocal; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy