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

com.gitee.huanminabc.utils_common.multithreading.cas.AtomicStringUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5-RELEASE
Show newest version
package com.gitee.huanminabc.utils_common.multithreading.cas;


import com.gitee.huanminabc.utils_common.multithreading.utils.SleepTools;

import java.util.concurrent.atomic.AtomicReference;

//AtomicReference的方式是直接替换地址
public class AtomicStringUtil {

    private AtomicReference atomicReference; //@TODO


    /**
     *  以原子的方式修改值 ,没有任何副作用(线程安全的)  (有ABA问题)
     */
    public void updateAndGet(String newValue) {
        while (!atomicReference.compareAndSet(atomicReference.get(), newValue)) {
            SleepTools.ms(10);
        }
    }

    /**
     * @param str 设置初始值
     */
    public void set(String str) {
        atomicReference.set(str);
    }

    //返回所有添加的和
    public String get() {
        return atomicReference.get();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy