Java™ Platform
Standard Ed. 7
 
java.util.concurrent.atomic

Class AtomicInteger

  • All Implemented Interfaces:
    Serializable

    public class AtomicInteger
    extends Number
    implements Serializable
    An int value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
    Since:
    1.5
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor and Description
      AtomicInteger()

      Creates a new AtomicInteger with initial value 0.
      AtomicInteger(int initialValue)

      Creates a new AtomicInteger with the given initial value.
    • Method Summary

      Methods 
      Modifier and Type Method and Description
      int addAndGet(int delta)

      Atomically adds the given value to the current value.
      boolean compareAndSet(int expect, int update)

      Atomically sets the value to the given updated value if the current value == the expected value.
      int decrementAndGet()

      Atomically decrements by one the current value.
      double doubleValue()

      Returns the value of the specified number as a double.
      float floatValue()

      Returns the value of the specified number as a float.
      int get()

      Gets the current value.
      int getAndAdd(int delta)

      Atomically adds the given value to the current value.
      int getAndDecrement()

      Atomically decrements by one the current value.
      int getAndIncrement()

      Atomically increments by one the current value.
      int getAndSet(int newValue)

      Atomically sets to the given value and returns the old value.
      int incrementAndGet()

      Atomically increments by one the current value.
      int intValue()

      Returns the value of the specified number as an int.
      void lazySet(int newValue)

      Eventually sets to the given value.
      long longValue()

      Returns the value of the specified number as a long.
      void set(int newValue)

      Sets to the given value.
      String toString()

      Returns the String representation of the current value.
      boolean weakCompareAndSet(int expect, int update)

      Atomically sets the value to the given updated value if the current value == the expected value.
    • Constructor Detail

      • AtomicInteger

        public AtomicInteger(int initialValue)
        Creates a new AtomicInteger with the given initial value.
        Parameters:
        initialValue - the initial value
      • AtomicInteger

        public AtomicInteger()
        Creates a new AtomicInteger with initial value 0.
    • Method Detail

      • get

        public final int get()
        Gets the current value.
        Returns:
        the current value
      • set

        public final void set(int newValue)
        Sets to the given value.
        Parameters:
        newValue - the new value
      • lazySet

        public final void lazySet(int newValue)
        Eventually sets to the given value.
        Parameters:
        newValue - the new value
        Since:
        1.6
      • getAndSet

        public final int getAndSet(int newValue)
        Atomically sets to the given value and returns the old value.
        Parameters:
        newValue - the new value
        Returns:
        the previous value
      • compareAndSet

        public final boolean compareAndSet(int expect,
        int update)
        Atomically sets the value to the given updated value if the current value == the expected value.
        Parameters:
        expect - the expected value
        update - the new value
        Returns:
        true if successful. False return indicates that the actual value was not equal to the expected value.
      • weakCompareAndSet

        public final boolean weakCompareAndSet(int expect,
        int update)
        Atomically sets the value to the given updated value if the current value == the expected value.

        May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

        Parameters:
        expect - the expected value
        update - the new value
        Returns:
        true if successful.
      • getAndIncrement

        public final int getAndIncrement()
        Atomically increments by one the current value.
        Returns:
        the previous value
      • getAndDecrement

        public final int getAndDecrement()
        Atomically decrements by one the current value.
        Returns:
        the previous value
      • getAndAdd

        public final int getAndAdd(int delta)
        Atomically adds the given value to the current value.
        Parameters:
        delta - the value to add
        Returns:
        the previous value
      • incrementAndGet

        public final int incrementAndGet()
        Atomically increments by one the current value.
        Returns:
        the updated value
      • decrementAndGet

        public final int decrementAndGet()
        Atomically decrements by one the current value.
        Returns:
        the updated value
      • addAndGet

        public final int addAndGet(int delta)
        Atomically adds the given value to the current value.
        Parameters:
        delta - the value to add
        Returns:
        the updated value
      • toString

        public String toString()
        Returns the String representation of the current value.
        Overrides:
        toString in class Object
        Returns:
        the String representation of the current value.
      • intValue

        public int intValue()
        Description copied from class: Number
        Returns the value of the specified number as an int. This may involve rounding or truncation.
        Specified by:
        intValue in class Number
        Returns:
        the numeric value represented by this object after conversion to type int.
      • longValue

        public long longValue()
        Description copied from class: Number
        Returns the value of the specified number as a long. This may involve rounding or truncation.
        Specified by:
        longValue in class Number
        Returns:
        the numeric value represented by this object after conversion to type long.
      • floatValue

        public float floatValue()
        Description copied from class: Number
        Returns the value of the specified number as a float. This may involve rounding.
        Specified by:
        floatValue in class Number
        Returns:
        the numeric value represented by this object after conversion to type float.
      • doubleValue

        public double doubleValue()
        Description copied from class: Number
        Returns the value of the specified number as a double. This may involve rounding.
        Specified by:
        doubleValue in class Number
        Returns:
        the numeric value represented by this object after conversion to type double.
Java™ Platform
Standard Ed. 7
 

Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Class AtomicInteger的更多相关文章

  1. AtomicInteger源码注释

    AtomicInteger源码 在java.util.concurrent.atomic包下提供了大量的原子类,这里以AtomicInteger源码为例,添加了一些注释,个人理解,供参考: 其中比较重 ...

  2. java API:AtomicInteger

    An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati ...

  3. AtomicInteger

    原子量和普通变量相比,主要体现在读写的线程安全上.对原子量的写是原子的,由CAS操作保证原子性.对原子量的读可以读到最新值,由volatile关键字来保证可见性. ublic class Atomic ...

  4. AtomicInteger简介

    这个类真的非常实用,更重要的是 它确实非常简单: 附上自己的代码,可以自己试试: AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的 ...

  5. Java AtomicInteger

    AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...

  6. AtomicInteger源码分析

    问题背景 最近在看LinkedBlockingQueue看到了其中的count使用AtomicInteger修饰,之前也看过AtomicInteger的一些解释,也是似懂非懂的,今天深入的了解了其实现 ...

  7. AtomicInteger源码分析——基于CAS的乐观锁实现

    AtomicInteger源码分析——基于CAS的乐观锁实现 1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时 ...

  8. AtomicInteger类保证线程安全的用法

    J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...

  9. java多线程之AtomicInteger

    AtomicInteger原子操作实现同步 package Thread.Common; import java.util.Timer; import java.util.TimerTask; imp ...

  10. Java自增原子性问题(测试Volatile、AtomicInteger)

    这是美团一面面试官的一个问题,后来发现这是一道面试常见题,怪自己没有准备充分:i++;在多线程环境下是否存在问题?当时回答存在,接着问,那怎么解决?...好吧,我说加锁或者synchronized同步 ...

随机推荐

  1. 「客户成功故事」OneAPM 助力网上办事大厅构建阳光、高效、安全的政务服务平台

    (一) 项目背景: 网上办事大厅是由省信息中心承建的电子政务核心业务系统,致力于为全省民众提供一站式网上办事服务,实现了政务信息网上公开.法人及个人事项网上办理.公共决策网上互动.政府效能网上监督五大 ...

  2. HashMap和Hashtable的同和不同(详细比较)

    一.综述 可以直接根据hashcode值判断两个对象是否相等吗?肯定是不可以的,因为不同的对象可能会生成相同的hashcode值.虽然不能根据hashcode值判断两个对象是否相等,但是可以直接根据h ...

  3. MyBatis笔记----mybatis分页

    mybatis版本3.4以下 结构 spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  4. AI学习---分类算法[K-近邻 + 朴素贝叶斯 + 决策树 + 随机森林 ]

    分类算法:对目标值进行分类的算法    1.sklearn转换器(特征工程)和预估器(机器学习)    2.KNN算法(根据邻居确定类别 + 欧氏距离 + k的确定),时间复杂度高,适合小数据    ...

  5. SQL server 数据库的索引和视图、存储过程和触发器

    1.索引:数据排序的方法,快速查询数据 分类: 唯一索引:不允许有相同值 主键索引:自动创建的主键对应的索引,命令方式不可删 聚集索引:物理顺序与索引顺序一致,只能创建一个 非聚集索引:物理顺序与索引 ...

  6. vue使用JS的形式进行路由导航

    // 注意: 一定要区分 this.$route 和 this.$router 这两个对象, // 其中: this.$route 是路由[参数对象],所有路由中的参数, params, query ...

  7. 【CQOI2006】凸多边形

    1713 -- [CQOI2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input ...

  8. 设计模式のMementoPattern(备忘录模式)----行为模式

    一.产生背景 意图:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态. 主要解决:所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态, ...

  9. 新手PHP连接MySQL数据库出问题(Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES))

    我用的环境是wampServer集成的软件包 在php连接MySQL数据库的时候老是出现这个问题Warning: mysqli_connect(): (HY000/1045): Access deni ...

  10. 复制数据库的Shell命令

    mysqldump -h$host db_old -uroot -p$pass | mysql -h$host db_new -uroot -p$pass 管道符号,是unix一个很强大的功能,符号为 ...