zoukankan      html  css  js  c++  java
  • IntUnaryOperator

    将函数,变为,参数传入

    1.  是一个接口,就是将一个对int类型的操作当一个参数传入进来。

      关键函数:applyAsInt(int operand)

      对一个参数operand进行一些操作,最后返回个int类型。

    package _4;
    
    import java.util.function.IntUnaryOperator;
    
    public class MyIntUnaryOperator {
        private static int value = 1;
    
    
        public static void main(String[] args) {
    
            My1 my1 = new My1();
    
            System.out.println(getAndUpdate(my1));
        }
    
        //将这个操作当成参数传入,
        public static int getAndUpdate(IntUnaryOperator intUnaryOperator){
            int next ;
            //获取一个value,对他进行一些操作,直接得到next
            next = intUnaryOperator.applyAsInt(MyIntUnaryOperator.value);
    
            return next;
        }
    
    
        //定义一个类实现这个接口
        public static class My1 implements  IntUnaryOperator{
    
            @Override
            public int applyAsInt(int operand) {
    
                return operand + 1;
            }
        }
    }

    2.  在AtomicInteger中的应用

      首先获取到原来的值,然后,进行一些操作,最后更新它

    next = updateFunction.applyAsInt(prev); 获取并计算新的结果,最后调用compareAndset函数进行更新操作。。
     public final int getAndUpdate(IntUnaryOperator updateFunction) {
            int prev, next;
            do {
                prev = get();
                next = updateFunction.applyAsInt(prev);
            } while (!compareAndSet(prev, next));
            return prev;
        }
  • 相关阅读:
    微信发送模板消息
    主从复制 读写分离
    php nginx反向代理
    go开发工具goclipse的安装
    安装go1.11.2
    基于科大讯飞AIUI平台自定义语义库的开发
    转载--php 7.2 安装 mcrypt 扩展
    mysql取出字段数据的精度
    sublime 2 格式化json
    RESTful接口需知道
  • 原文地址:https://www.cnblogs.com/da-peng/p/9899633.html
Copyright © 2011-2022 走看看