Class AtomicInteger
Class AtomicInteger
- java.lang.Object
- java.lang.Number
- java.util.concurrent.atomic.AtomicInteger
- All Implemented Interfaces:
- Serializable
public class AtomicInteger
extends Number
implements SerializableAnintvalue that may be updated atomically. See thejava.util.concurrent.atomicpackage specification for description of the properties of atomic variables. AnAtomicIntegeris used in applications such as atomically incremented counters, and cannot be used as a replacement for anInteger. However, this class does extendNumberto 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 value0.AtomicInteger(int initialValue)Creates a new AtomicInteger with the given initial value.
Method Summary
Methods Modifier and Type Method and Description intaddAndGet(int delta)Atomically adds the given value to the current value.booleancompareAndSet(int expect, int update)Atomically sets the value to the given updated value if the current value==the expected value.intdecrementAndGet()Atomically decrements by one the current value.doubledoubleValue()Returns the value of the specified number as adouble.floatfloatValue()Returns the value of the specified number as afloat.intget()Gets the current value.intgetAndAdd(int delta)Atomically adds the given value to the current value.intgetAndDecrement()Atomically decrements by one the current value.intgetAndIncrement()Atomically increments by one the current value.intgetAndSet(int newValue)Atomically sets to the given value and returns the old value.intincrementAndGet()Atomically increments by one the current value.intintValue()Returns the value of the specified number as anint.voidlazySet(int newValue)Eventually sets to the given value.longlongValue()Returns the value of the specified number as along.voidset(int newValue)Sets to the given value.StringtoString()Returns the String representation of the current value.booleanweakCompareAndSet(int expect, int update)Atomically sets the value to the given updated value if the current value==the expected value.Methods inherited from class java.lang.Number
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 value0.
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 valueupdate- 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 valueupdate- 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.
intValue
public int intValue()
Description copied from class:NumberReturns the value of the specified number as anint. This may involve rounding or truncation.
longValue
public long longValue()
Description copied from class:NumberReturns the value of the specified number as along. This may involve rounding or truncation.
floatValue
public float floatValue()
Description copied from class:NumberReturns the value of the specified number as afloat. This may involve rounding.- Specified by:
floatValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
float.
doubleValue
public double doubleValue()
Description copied from class:NumberReturns the value of the specified number as adouble. This may involve rounding.- Specified by:
doubleValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
double.
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的更多相关文章
- AtomicInteger源码注释
AtomicInteger源码 在java.util.concurrent.atomic包下提供了大量的原子类,这里以AtomicInteger源码为例,添加了一些注释,个人理解,供参考: 其中比较重 ...
- java API:AtomicInteger
An int value that may be updated atomically. See the java.util.concurrent.atomic package specificati ...
- AtomicInteger
原子量和普通变量相比,主要体现在读写的线程安全上.对原子量的写是原子的,由CAS操作保证原子性.对原子量的读可以读到最新值,由volatile关键字来保证可见性. ublic class Atomic ...
- AtomicInteger简介
这个类真的非常实用,更重要的是 它确实非常简单: 附上自己的代码,可以自己试试: AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的 ...
- Java AtomicInteger
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...
- AtomicInteger源码分析
问题背景 最近在看LinkedBlockingQueue看到了其中的count使用AtomicInteger修饰,之前也看过AtomicInteger的一些解释,也是似懂非懂的,今天深入的了解了其实现 ...
- AtomicInteger源码分析——基于CAS的乐观锁实现
AtomicInteger源码分析——基于CAS的乐观锁实现 1. 悲观锁与乐观锁 我们都知道,cpu是时分复用的,也就是把cpu的时间片,分配给不同的thread/process轮流执行,时间片与时 ...
- AtomicInteger类保证线程安全的用法
J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...
- java多线程之AtomicInteger
AtomicInteger原子操作实现同步 package Thread.Common; import java.util.Timer; import java.util.TimerTask; imp ...
- Java自增原子性问题(测试Volatile、AtomicInteger)
这是美团一面面试官的一个问题,后来发现这是一道面试常见题,怪自己没有准备充分:i++;在多线程环境下是否存在问题?当时回答存在,接着问,那怎么解决?...好吧,我说加锁或者synchronized同步 ...
随机推荐
- ORACLE Index Lookup索引访问路径总结
在ORACLE中,索引访问/查找(Index Lookup)路径有五种方式,分别为INDEX UNIQUE SCAN.INDEX RANGE SCAN.INDEX FULL SCAN.INDEX FA ...
- SQL Server如何用触发器捕获DML操作的会话信息
需求背景 上周遇到了这样一个需求,维护人员发现一个表的数据经常被修改,由于历史原因:文档缺少:以及维护人员的经常变更,导致他们对系统也业务也不完全熟悉,他们也不完全清楚哪些系统和应用程序会对这个表的数 ...
- cookie 和session 的区别(转)
二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有 ...
- Not Found woff 字体库
原因是,默认在IIS上是没有添加对*.woff,*.svg文件的Mime类型,因此在客户端请求此类文件时得到的都是404. 所以我们只需要在我们对应网站下的Mime类型中添加文件对应的类型就行了 .w ...
- 介绍一个比较了各种浏览器对于HTML5 等标准支持程度的网站
可以选择浏览器种类,版本,比较的功能 网站地址:https://caniuse.com/#comparison
- vue 之 .sync 修饰符
在一些情况下,我们可能会需要对一个 prop (父子组件传递数据的属性) 进行“双向绑定”. 在vue 1.x 中的 .sync 修饰符所提供的功能.当一个子组件改变了一个带 .sync 的prop的 ...
- 比较两个List是否相等,长度和内容都相等
/** *list中存在重复数据,且顺序不一样*/import java.util.ArrayList; import java.util.Collections; import java.util. ...
- vue-cli笔记
总是忘,留个笔记. vue-cli A simple CLI for scaffolding Vue.js projects. Installation Prerequisites: Node.js ...
- 基于vue-cli的vue项目移动端样式适配,lib-flexible和postcss-px2rem
安装 flexible和 postcss-px2rem(命令行安装) npm install lib-flexible --save npm install postcss-px2rem --save ...
- Gps定位和wifi定位和基站定位的比较
现在手机定位的方式是:Gps定位,wifi定位,基站定位 Gps定位的前提,手机开启Gps定位模块,在室外,定位的精度一般是几米的范围 wifi定位的前提,手机要开启wifi,连不连上wifi热点都可 ...