AtomicInteger类 7 _- }% B# D5 [8 _ 2 K+ ?3 H" U- h+ J这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。- Z5 y6 z6 j1 i( [6 J F3 D
' R/ ~% s S, z3 n3 S
重点内容0 w- y; Y3 U* n
" J; m: C: S5 [" j& X/ H2 W1 \属性:: \' e, b1 G' D$ D/ _- n, w
private static final Unsafe unsafe = Unsafe.getUnsafe(); / r6 T. x0 Q9 e: I; ]. q- w8 q: H7 O 7 A& A6 Q6 [0 J: cprivate static final long valueOffset; 0 p1 N4 A4 _- K0 G. h# J3 ` $ a9 P. C* ~" F) S& ^private volatile int value; 8 q [! ?# d- r3 |! ^ K , A! S/ j' a6 e静态代码块: 1 J0 ?3 _! K; K2 \6 T% rstatic {% E* l( E f3 h$ ]; r1 b
try {' z5 c, e3 n+ _8 r4 T" ~
valueOffset = unsafe.objectFieldOffset 0 u: _" y5 r+ I: d9 r (AtomicInteger.class.getDeclaredField("value")); $ b8 l! j2 [! g } catch (Exception ex) { throw new Error(ex); }3 [7 p" K: }, W' k, K" q @. g/ G
} ; b: y( @" |. j2 s' j--------------------- + y; O3 m. M( U. y* v V构造方法 2 q' z0 @5 J; |( cpublic AtomicInteger() { / ]6 {1 Q2 Z& {' N } % `+ j; D' g" U% B( X7 @; Q4 S 8 \0 I$ o4 z4 e L$ tpublic AtomicInteger(int initialValue) {+ g y# d1 s ~# Y4 i t
value = initialValue; ) D( O, h* g% ` }9 j* I4 ~0 }; u1 z4 e
& v# h2 w7 S. h1 _/ h5 R0 U对象方法: ! ~; P$ g j( y2 R6 `; b public final int get() { 1 K1 I1 j0 X: t3 | return value; 9 A/ E# l& d- ~ _. d- h! o. v- A } 4 O, m' N2 Q( [9 R8 T9 t% z G! E7 u: [$ Q/ I3 I$ g
s' I% d6 x1 A. K; Apublic final void set(int newValue) { / m) m* k! d% ~7 g, c8 \" f- Y value = newValue;& r7 y& e s* [7 y7 k1 ~/ j' f
}( f2 p5 ?0 _2 T, _
& o) `. I9 ^) J3 D4 s2 F
) `+ u1 a& C8 v, U, epublic final void lazySet(int newValue) { : M9 }, S" v. s( w1 p unsafe.putOrderedInt(this, valueOffset, newValue); 3 M& ^2 V9 q& L" f7 q }. w* ]6 F4 n) I7 F1 ~
1 T" \& v9 Q: T) g% F9 I! p2 Y$ O6 o2 }, l
public final int getAndSet(int newValue) { 6 G( u) ?4 x4 ?/ P. _6 [ for (;;) { ; ^* P9 m0 G" l7 h7 F! B, s' W1 s int current = get();5 X7 _' o8 R3 }
if (compareAndSet(current, newValue)) 3 P$ @" i" r% X5 i6 y) S* W. J return current; $ v3 F5 W& w4 Z6 J6 Q) U! j4 K } , `- W9 n; a* G5 ^% m }" q: r$ ~4 V( H4 f2 t V
$ a3 @' `. O F9 k 5 j- }( T. g! y; fpublic final boolean compareAndSet(int expect, int update) { . W9 o$ W4 U0 }& t. \6 M2 V+ m return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 5 E. F' @2 I1 p0 V' X } / ~' O" h. ?2 w# v " \3 E! D0 D% m( U, ?/ g ; l2 y, @8 z& n9 c/ B" apublic final boolean weakCompareAndSet(int expect, int update) { , g: ~2 L) f3 ] return unsafe.compareAndSwapInt(this, valueOffset, expect, update);$ A' u# y4 y4 ~0 X+ F
} ! H- x$ d, ^) E% f( L2 T0 {/ p% q" [9 M
public final int getAndIncrement() {/ z+ g6 `4 K# Q7 A: N# k
for (;;) {% x7 J/ c3 W- e% t: J
int current = get(); f# h; F( A1 Y; L, [8 T
int next = current + 1; ) Z) S! O7 N, ^( T( I if (compareAndSet(current, next)) ; V9 |% H% C9 K b% t0 V! w, K8 B return current;$ S) M; D! S: Q3 J
} " F& `& q3 a. {4 B }1 N. B9 d T$ J p1 {/ v
% K/ f1 }% O7 i/ O& q* ~public final int getAndDecrement() { 8 K, E6 W, P0 I for (;;) { - y' m8 s8 f$ Z% d$ s6 }; G+ e int current = get(); 5 c9 m* {) }6 S, F int next = current - 1;# E c* \3 f4 ?) o
if (compareAndSet(current, next)) / ?; a% A$ F% i x$ ]/ W return current; 1 N8 ^0 L# z* b6 D, _: p- H" R* | } ) U, g/ p2 N Y* U0 T3 E) w }2 k+ V4 k) R" I8 @( i
3 k x$ I. J! ^8 E: J# `6 m9 r% d8 a$ D- U
public final int getAndAdd(int delta) { 5 D1 t+ R$ T" w4 H# R( P* f for (;;) {# l0 c+ K6 d1 M
int current = get();& B6 n4 `8 _7 i
int next = current + delta; 3 b: I$ w: x u" k* u0 c if (compareAndSet(current, next))4 K1 C' ]9 F+ U" F
return current;) |, n6 f' H7 r4 s% T) q# U
}+ f2 d$ \# x. A( F" n
}9 B2 b \( q' p) ]7 l( ?& F8 B+ H6 j3 D P
, W t% D+ C* q& S' e9 V! [ 6 h) ~: e0 @" j0 k7 Ppublic final int incrementAndGet() {8 r: X. [1 q" Z/ Y! p- y# ?. U, F
for (;;) {7 Z9 d' Q n: P+ }2 g6 e! w
int current = get(); : Y D$ P7 P. Z' k4 J* ^2 `: q j int next = current + 1; % c7 u: L2 T/ C7 C if (compareAndSet(current, next)) 9 Z( K( {9 |+ K7 ] return next; 2 ^- \: |4 c. u4 V } , b* v0 h L' S }% n r5 }9 ^2 K
0 P- y, T( ]5 r9 u6 y* h3 b V$ z
+ L* U+ |' O1 N0 Y& H$ j; _4 \# _5 Tpublic final int decrementAndGet() { ' z7 \1 A- S8 \% g- i0 ? for (;;) { . L# B0 R6 l7 K8 {3 m$ u+ l! p. k* \* D int current = get(); 2 u1 ^+ Y3 p6 ^/ z% D y int next = current - 1; 7 l- D D# e; N7 b9 ?/ G8 Z0 O if (compareAndSet(current, next)) ( v+ d+ n+ i. N' M7 c* t return next;0 s7 h3 g1 t( s4 s9 r E' G
} / G, s1 b7 G$ Y0 O4 P. x' u }5 U) t# H: G) A0 W7 g; }; e3 w
d1 ~# O$ b# g9 C9 |* X8 g' p4 b! ]) ?! Y U
public final int addAndGet(int delta) { * [+ m# f* e" P0 U. n! G8 ^ for (;;) {% M5 M# w8 e8 r' r4 q- `8 b
int current = get(); ) }: n% G: h6 [. ?1 u( t8 a int next = current + delta; 2 X, D t9 }( Y) F. I if (compareAndSet(current, next)) # c; y" z8 n" H( s: ]( _ return next; . [6 k3 w: e. _2 o% } } 9 ]7 W0 _6 A! {% x9 n }3 b" ]1 ]9 S" a
: z" J7 [) V' v: n4 L9 t6 o1 x: o7 E. l+ D# d3 V
public String toString() { 3 Z% ~ v2 o6 y5 M! h return Integer.toString(get()); ) Q+ Q) U0 ]3 [3 m T } 9 D# R+ D' R; e' d! ^5 J& X6 f' Z. t, t. m! V
1 v& Z; J2 l }9 N3 h2 Apublic int intValue() {& a3 u) t. L7 Y0 r. }- y( G
return get();# c: f! [: b1 g4 C
}0 n4 J7 F" z" h# I; u
1 G" l: p" ? s" Y7 ?9 S% A
5 c! \6 i' y( \( F6 V. [7 A" Upublic long longValue() {: m0 I+ y3 ]6 ^% ~- l) p
return (long)get(); U8 W* ?6 t+ m g6 _/ Q# m
} 7 V- {. y7 Q, D0 O L$ F2 v; J! V+ U/ h5 h. U$ w0 U# z c
% g: ?4 k5 c3 x1 k
public float floatValue() { ; j3 ]+ j/ t2 ^ return (float)get();/ G0 n* A% o" D/ ]& Q
} ; {7 s! B2 o9 }+ h ! q! Y1 H8 K: l) @- X% T1 P ( }+ _7 h) f1 z: n* l! P5 tpublic double doubleValue() {& d) P3 U$ e/ q- n
return (double)get(); 9 y, b5 |1 {% _6 A% Z. d0 J } 6 \6 C. w+ O6 c$ Y( v--------------------- . c% o I; _/ }( d9 e5 s: M* \/ [
作者:feyshine 7 P A' b6 _& g4 b) ~2 Y2 L+ q
来源:CSDN " t5 B! K( V, ?: B8 g8 ~" }, i% C$ o+ d& F( r
5 e* L. C! y6 o/ [7 u+ G* q . e% i& T" O6 }: U6 {( R . V E) ^6 V8 L( u/ j0 l7 f+ }/ I) J- V4 N! a: c