AtomicInteger类
来源:互联网 发布:it strikes me 编辑:程序博客网 时间:2024/06/05 15:45
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。
重点内容
- 属性:
private static final Unsafe unsafe = Unsafe.getUnsafe();private static final long valueOffset;private volatile int value;
- 静态代码块:
static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } }
- 构造方法
public AtomicInteger() { }public AtomicInteger(int initialValue) { value = initialValue; }
- 对象方法:
public final int get() { return value; }public final void set(int newValue) { value = newValue; }public final void lazySet(int newValue) { unsafe.putOrderedInt(this, valueOffset, newValue); }public final int getAndSet(int newValue) { for (;;) { int current = get(); if (compareAndSet(current, newValue)) return current; } }public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); }public final boolean weakCompareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); }public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } }public final int getAndDecrement() { for (;;) { int current = get(); int next = current - 1; if (compareAndSet(current, next)) return current; } }public final int getAndAdd(int delta) { for (;;) { int current = get(); int next = current + delta; if (compareAndSet(current, next)) return current; } }public final int incrementAndGet() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return next; } }public final int decrementAndGet() { for (;;) { int current = get(); int next = current - 1; if (compareAndSet(current, next)) return next; } }public final int addAndGet(int delta) { for (;;) { int current = get(); int next = current + delta; if (compareAndSet(current, next)) return next; } }public String toString() { return Integer.toString(get()); }public int intValue() { return get(); }public long longValue() { return (long)get(); }public float floatValue() { return (float)get(); }public double doubleValue() { return (double)get(); }
0 0
- AtomicInteger类
- AtomicInteger类
- java AtomicInteger 类学习
- Java AtomicInteger 类
- AtomicInteger相关类
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- AtomicInteger
- 利用机器学习进行恶意代码分类
- poj1860解题报告(两种解法)
- 黑马程序员——oc加强学习(内存管理)
- C语言结构体内存对齐问题
- 1031. Campus
- AtomicInteger类
- 回文素数
- Ubuntu 12.10 old-releases更新源
- BPL插件框架的二种实现方法
- DTLS分析
- python爬虫抓站的一些技巧
- IOC和DI
- 处理正片和负片
- Android获得当前正在运行的Activity及相关信息