📌  相关文章
📜  Java中的 AtomicLong weakCompareAndSet() 方法及示例

📅  最后修改于: 2022-05-13 01:54:41.424000             🧑  作者: Mango

Java中的 AtomicLong weakCompareAndSet() 方法及示例

Java.util.concurrent.atomic.AtomicLong.weakCompareAndSet()是Java中的一个内置方法,如果当前值等于参数中传递的预期值,则该方法将值设置为参数中传递的值。该函数返回一个布尔值,它让我们知道更新是否完成。

句法:

public final boolean weakCompareAndSet(long expect,
                                       long val)

参数:该函数接受两个强制性参数,如下所述:

  • 期望:指定原子对象应该是的值。
  • val:指定如果 atomic long 等于 expect 时要更新的值。

返回值:函数返回一个布尔值,成功则返回true,否则返回false。

下面的程序说明了上述方法:

方案一:

Java
// Java program that demonstrates
// the weakCompareAndSet() function
 
import java.util.concurrent.atomic.AtomicLong;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
 
        // Prlongs the updated value
        System.out.println("Previous value: "
                           + val);
 
        // Checks if previous value was 0
        // and then updates it
        boolean res
            = val.weakCompareAndSet(0, 6);
 
        // Checks if the value was updated.
        if (res)
            System.out.println("The value was "
                               + "updated and it is "
                               + val);
        else
            System.out.println("The value was "
                               + "not updated");
    }
}


Java
// Java program that demonstrates
// the weakCompareAndSet() function
 
import java.util.concurrent.atomic.AtomicLong;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
 
        // Prlongs the updated value
        System.out.println("Previous value: "
                           + val);
 
        // Checks if previous value was 0
        // and then updates it
        boolean res
            = val.weakCompareAndSet(10, 6);
 
        // Checks if the value was updated.
        if (res)
            System.out.println("The value was "
                               + "updated and it is "
                               + val);
        else
            System.out.println("The value was "
                               + "not updated");
    }
}


输出:
Previous value: 0
The value was updated and it is 6

方案二:

Java

// Java program that demonstrates
// the weakCompareAndSet() function
 
import java.util.concurrent.atomic.AtomicLong;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Initially value as 0
        AtomicLong val = new AtomicLong(0);
 
        // Prlongs the updated value
        System.out.println("Previous value: "
                           + val);
 
        // Checks if previous value was 0
        // and then updates it
        boolean res
            = val.weakCompareAndSet(10, 6);
 
        // Checks if the value was updated.
        if (res)
            System.out.println("The value was "
                               + "updated and it is "
                               + val);
        else
            System.out.println("The value was "
                               + "not updated");
    }
}
输出:
Previous value: 0
The value was not updated

参考: https: Java/util/concurrent/atomic/AtomicLong.html#weakCompareAndSet–