AtomicInteger 学习
2017-04-19 10:20
363 查看
AtomicInteger,一个提供原子操作的Integer的类。在Java语言中,为了保证一个数字的更新只能被一个类获取,在使用的时候,不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。该类的类中方法无法被重载,都添加了final关键字
//获取当前的值
public final int get()
//取当前的值,并设置新的值
public final int getAndSet(int newValue)
//获取当前的值,并自增
public final int getAndIncrement()
//获取当前的值,并自减
public final int getAndDecrement()
//获取当前的值,并加上预期的值
public final int getAndAdd(int delta)
代码学习:
考究他们的性能
package zl.study.concurrency;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerCompareTest {
private int value;
public AtomicIntegerCompareTest(int value){
this.value = value;
}
public synchronized int increase(){
return value++;
}
public static void main(String args[]){
long start = System.currentTimeMillis();
AtomicIntegerCompareTest test = new AtomicIntegerCompareTest(0);
for( int i=0;i< 1000000;i++){
test.increase();
}
long end = System.currentTimeMillis();
System.out.println("time elapse:"+(end -start));
long start1 = System.currentTimeMillis();
AtomicInteger atomic = new AtomicInteger(0);
for( int i=0;i< 1000000;i++){
atomic.incrementAndGet();
}
long end1 = System.currentTimeMillis();
System.out.println("time elapse:"+(end1 -start1) );
}
}
time elapse:31
time elapse:16
由此不难看出,通过JNI本地的CAS性能远超synchronized关键字
//获取当前的值
public final int get()
//取当前的值,并设置新的值
public final int getAndSet(int newValue)
//获取当前的值,并自增
public final int getAndIncrement()
//获取当前的值,并自减
public final int getAndDecrement()
//获取当前的值,并加上预期的值
public final int getAndAdd(int delta)
代码学习:
考究他们的性能
package zl.study.concurrency;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerCompareTest {
private int value;
public AtomicIntegerCompareTest(int value){
this.value = value;
}
public synchronized int increase(){
return value++;
}
public static void main(String args[]){
long start = System.currentTimeMillis();
AtomicIntegerCompareTest test = new AtomicIntegerCompareTest(0);
for( int i=0;i< 1000000;i++){
test.increase();
}
long end = System.currentTimeMillis();
System.out.println("time elapse:"+(end -start));
long start1 = System.currentTimeMillis();
AtomicInteger atomic = new AtomicInteger(0);
for( int i=0;i< 1000000;i++){
atomic.incrementAndGet();
}
long end1 = System.currentTimeMillis();
System.out.println("time elapse:"+(end1 -start1) );
}
}
time elapse:31
time elapse:16
由此不难看出,通过JNI本地的CAS性能远超synchronized关键字
相关文章推荐
- JUC学习之AtomicInteger
- Java并发学习(八)-AtomicIntegerArray数组类型类
- Java并发学习(九)-AtomicIntegerFieldUpdater字段原子更新类
- 高级并发编程学习-atomic包学习(重点介绍AtomicInteger、AtomicIntegerFieldUpdater)
- muduo网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
- Java并发学习(七)-AtomicInteger基本数据类型类
- AtomicInteger学习笔记
- java AtomicInteger 类学习
- muduo 3 网络库学习之Timestamp类、AtomicIntegerT 类封装中的知识点
- AtomicInteger的用法
- synchronized和AtomicInteger解决并发问题的性能比较
- AtomicInteger的使用
- JUC源码分析2-原子变量-AtomicIntegerArray/AtomicLongArray/AtomicReferenceArray
- java核心基础--jdk源码分析学习--Integer
- Java的多线程编程模型5--从AtomicInteger开始
- Android的图片,字符串,demin,color,以及Array,boolean,Integer资源的使用-android学习之旅(五十四)
- Java的多线程编程模型5--从AtomicInteger开始
- AtomicInteger的使用,多线程叠加或叠减
- Java学习_int和Integer的区别+包装类
- java.util.concurrent.atomic 并发包下的原子操作类(AtomicBoolean,AtomicInteger,AtomicLong......))