热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中的AtomicLongArraygetAndAccumulate()方法,示例

Java中的AtomicLongArraygetAndAccumulate()方法,示例

Java 中的 AtomicLongArray getAndAccumulate()方法,示例

原文:https://www . geeksforgeeks . org/atomicongarray-getandaccumulate-method-in-Java-with-examples/

Java . util . concurrent . atomic . AtomicLongArray . getandaccumulate()是 Java 中的一个内置方法,它在将给定函数应用于当前值和给定值后,用结果自动更新 atomicongarray 的任何索引处的值,返回前一个值。此方法接受要执行操作的原子克隆数组的索引、执行操作的值和累加器函数作为参数。应用该函数时,索引处的当前值作为其第一个参数,给定的更新作为第二个参数。累加器函数应该没有副作用,因为当由于线程之间的争用导致尝试的更新失败时,它可以被重新应用。函数 getAndAccumulate()accumulated gt()类似,但前者在更新前返回值,后者在更新后返回值。

语法:

公共最终长 getAndAccumulate(int i,long x,LongBinaryOperator 累加器函数)

参数:该函数接受三个参数:


  • I–要进行更新的索引。 x–用 I 处的值进行运算的值 accumulatorFunction – A side-effect-free function of two arguments.

    返回值:该函数返回更新前的值,该值在中。
    以下程序说明上述方法:
    程序 1:

    ```java
    // Java program that demonstrates
    // the getAndAccumulate() function

    import java.util.concurrent.atomic.AtomicLongArray;
    import java.util.function.LongBinaryOperator;

    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            long a[] = { 1, 2, 3, 4, 5 };

    // Initializing an AtomicLongArray with array a
            AtomicLongArray arr = new AtomicLongArray(a);

    // Displaying the AtomicLongArray
            System.out.println("The array : " + arr);

    // Index where update is to be made
            int idx = 4;

    // Value to make operation with value at idx
            long x = 5;

    // Declaring the accumulatorFunction
            LongBinaryOperator add = (u, v) -> u + v;

    // Updating the value at idx
            // applying getAndAccumulate
            long prev = arr.getAndAccumulate(idx, x, add);

    // The previous value at idx
            System.out.println("Value at index " + idx
                               + " before update is "
                               + prev);

    // Displaying the AtomicLongArray
            System.out.println("The array after update : "
                               + arr);
        }
    }
    ```

    Output:

    ```java
    The array : [1, 2, 3, 4, 5]
    Value at index 4 before update is 5
    The array after update : [1, 2, 3, 4, 10]

    ```

    程序 2:

    ```java
    // Java program that demonstrates
    // the getAndAccumulate() function

    import java.util.concurrent.atomic.AtomicLongArray;
    import java.util.function.LongBinaryOperator;

    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            long a[] = { 1, 2, 3, 4, 5 };

    // Initializing an AtomicLongArray with array a
            AtomicLongArray arr = new AtomicLongArray(a);

    // Displaying the AtomicLongArray
            System.out.println("The array : " + arr);

    // Index where update is to be made
            int idx = 0;

    // Value to make operation with value at idx
            long x = 6;

    // Declaring the accumulatorFunction
            LongBinaryOperator sub = (u, v) -> u - v;

    // Updating the value at idx
            // applying getAndAccumulate
            long prev = arr.getAndAccumulate(idx, x, sub);

    // The previous value at idx
            System.out.println("Value at index " + idx
                               + " before update is "
                               + prev);

    // Displaying the AtomicLongArray
            System.out.println("The array after update : "
                               + arr);
        }
    }
    ```

    Output:

    ```java
    The array : [1, 2, 3, 4, 5]
    Value at index 0 before update is 1
    The array after update : [-5, 2, 3, 4, 5]

    ```

    参考:
    https://docs . Oracle . com/javase/8/docs/API/Java/util/concurrent/atomic/atomicongarray . html # getAndAccumulate-int-long-Java . util . function . longbinaryoperator-



推荐阅读
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有