AtomicInteger类2 B' A: L) m v; ^% m) E
0 i5 K1 w. J- T- [8 D$ s8 f
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 N/ @( z2 c% C' z& o
$ x7 k9 V \( \
重点内容" x# W; ?# [& o# h
" g( a0 O# J8 p: C* M# g) |7 p2 f属性:6 K/ U" m! ]& s! g
private static final Unsafe unsafe = Unsafe.getUnsafe(); ; D! s7 T+ @, u3 w- h1 N7 r# S7 V, E: K, g! S
private static final long valueOffset;( |( z" N; ?1 B6 M1 \$ ^6 I
3 X% e; N) [: `- a9 |
private volatile int value;2 C$ T0 J- J1 u4 V/ f2 k5 P
" f0 t' a8 v+ O. u静态代码块:3 Y* N9 `$ S2 P6 P& Q) f5 q
static { + V" s: y+ p+ D) e7 {# i. q try {. s7 a3 Q6 r E
valueOffset = unsafe.objectFieldOffset , y8 Q$ a. j0 L/ P! V (AtomicInteger.class.getDeclaredField("value"));% y8 b) m$ D% O! m" f" z
} catch (Exception ex) { throw new Error(ex); } % H Z3 U! c! J& d) Y2 b+ u3 b }, o( M2 x/ c! F+ V% _
--------------------- 2 T. c0 ^1 m7 K构造方法 9 g2 A- N7 E: Rpublic AtomicInteger() {, R2 ?$ |! o" ]. F b
}/ f+ D6 @4 ?( Y) k0 s8 m+ _ ]
: v% q. V, Z: B
public AtomicInteger(int initialValue) { * t D; ~$ L7 p6 j4 c3 h value = initialValue; : A% g/ J. h9 K5 \ }8 M* i, N; h* Z; A! t2 \
& ~" f- V" h2 @' e5 O& j$ q3 S# j对象方法:* n& P. O& G+ _- [) Y; p9 w
public final int get() { : Z, h' ]5 g# ?6 Q0 A# M$ p( `4 w return value;% s ]' \/ Y* Y% u" D5 M4 u
}4 x0 [) \ h. `; w( a
0 P2 t+ L" d3 k
& p _$ C$ P' }+ P! w& s. f
public final void set(int newValue) {" |+ ?9 p$ Z" U; t: A E' V2 K
value = newValue; % ^4 r* u6 Q# q5 F7 s3 P3 S, n! { } " ]' H/ K+ A) |! L# l$ x" ]. P5 g) W& n9 }
, Z0 Z( c# d, I4 J# w
public final void lazySet(int newValue) {+ j# l( e' l- R4 {2 x" t
unsafe.putOrderedInt(this, valueOffset, newValue);; S' ~. w5 B1 x6 K) L
} 1 K) Y8 d" T8 a2 h6 b$ H. ?- | 4 }) x6 L0 ]2 [0 W; U1 ~3 u+ b4 U- ~6 C* S8 A+ x2 j
public final int getAndSet(int newValue) {/ N4 T% c! k' W
for (;;) {& D3 B: u2 Y+ g
int current = get(); 6 H* G, w2 z" p if (compareAndSet(current, newValue)) 5 |% P! O2 [6 y# F! o0 f$ C* d" P return current; d1 l b5 \( @/ B
}; H I ^8 d, o$ n6 f n
} + N3 ]$ J$ d# a8 S# k3 u; L; y4 E; f7 O$ {
2 d1 l" M) z; K- }* Spublic final boolean compareAndSet(int expect, int update) {5 ~" O6 k0 R& Y4 B. S/ q0 l' |
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);' B+ S7 e9 [" ?# D" Z# M/ W/ o
}3 R" i+ j" n9 _9 G
! X4 i" y6 l" ?& | * l( g; e& S' u+ e( ipublic final boolean weakCompareAndSet(int expect, int update) { + j( E, d5 ^& O7 e( ^: w return unsafe.compareAndSwapInt(this, valueOffset, expect, update);4 G! S$ X; C2 L' a, ]
} 8 ?8 E o" j2 K6 K9 M 1 u9 d4 \- I2 n, _ r, g! Kpublic final int getAndIncrement() {5 {$ B+ V( Z! b( z. _+ ]
for (;;) { ' ?( e, F( q- N! |7 i int current = get();) E% n: y6 I8 L: i# b
int next = current + 1;7 o! V( ?6 J! Q. D" Q( Q* c2 g
if (compareAndSet(current, next))& \ Q5 W. ]5 C- e% l+ w8 [
return current; ( ^+ O% f z" z$ G" u7 } } & S: K* X3 z9 B' b }/ A% P1 F' d+ G& g' d
" i! _9 x) j" p
public final int getAndDecrement() { & w& R& I( z& ?# ]7 T/ N& _* U$ i for (;;) {* [7 V& W2 r8 |9 m7 y9 }1 V
int current = get();# M/ p3 B7 w) t4 n' ^. F5 M
int next = current - 1;7 D4 a3 S) A }; C
if (compareAndSet(current, next))3 m3 e& F% h/ Q) j9 h# \4 m+ }
return current; ) P C8 M1 S" c2 S, J3 F' Q }6 p+ ^ y- R' A5 v* u3 i
} 2 [+ {4 i1 ^) y+ Y. z. p8 Q! ~6 p9 s! N; u2 r p4 \
% O0 \# T* ]0 e7 p) n' W; o& N
public final int getAndAdd(int delta) {! h$ ?" H; a! ]/ G
for (;;) { ; f5 p! Q) H" ^; Y int current = get();+ G3 u: e2 S, q7 ~- F* D0 u
int next = current + delta;2 c( t' C3 A9 h0 _) w
if (compareAndSet(current, next)) 2 n! b, e' N! @0 Q k8 H/ H return current; 8 k1 Y% E/ B5 @3 i/ q3 f* H7 g! \ }) Q3 f* I) u0 X- a
} ) X, H# Q# a# @ g9 n+ [. Y5 C- w( Q$ z8 c( M1 I8 O% }
' A5 W, d7 Q! s. rpublic final int incrementAndGet() { ' T8 }1 }6 i1 h2 n% ]5 _( L; l5 B for (;;) {0 y) U2 E% K& o, F; T" w6 b& z
int current = get(); ! J8 l9 q f: N int next = current + 1; 5 Z6 L7 z# O l7 p( b6 t if (compareAndSet(current, next)) $ C* A4 @3 L* \ q. L return next; # Q/ y9 a/ F0 L } 8 w2 S! J! z! S8 P2 {& w5 i. R. z }0 C& T# ? t* z, C$ A
; D, a- v3 m3 U6 k5 V$ z6 E
' j, z+ D5 G4 P; J5 \ Cpublic final int decrementAndGet() {0 B* I' w. J" E6 g
for (;;) { $ x8 p; t+ A# \$ k* M3 u% @. e% j int current = get(); . ^1 W* C" M) _ B- u' O int next = current - 1;3 w1 D" d2 j6 M. I$ E3 \$ s
if (compareAndSet(current, next)) ( E! k. _2 i9 _* B' z return next;3 C* J% K4 Q/ P% ~
} ) N: j4 ~7 G* j } " U% F! t5 }/ P; {! Z : s' i2 [( w# n5 H1 n5 B8 G& I* U $ Z4 H( k( \3 N8 v4 W2 a! Hpublic final int addAndGet(int delta) { 0 F3 ?. E; h. i9 ?) J) ~* B) u for (;;) { " W0 w) s* B, l; { int current = get(); Q/ x$ o, ?8 d4 N int next = current + delta; 7 W' Z& F. e( `: L* O2 L' l" p H if (compareAndSet(current, next))+ W- S" T! x3 Z {% l( O' h1 n6 m; J
return next;2 S/ p4 w# ~. z' S( A
}$ E+ e; X) S+ A- K J# H
}8 Y7 D! f( I# T* j9 i$ ^
4 T7 _/ P. Q6 X, Z
) t y2 d1 Y) `9 qpublic String toString() {( l4 |* Z! j% }8 _2 o
return Integer.toString(get()); , I3 n$ g" X& j1 Q) h } & S$ q: J0 u4 m# U! e ! r# J" N1 E* a$ c5 Y0 s7 ^4 F8 M7 ], ?1 n
public int intValue() { 9 W3 D+ D: M2 u' r/ | return get();( B4 }5 Z0 x3 i4 P! O
} : m6 U+ G3 h* j' N. M7 F & R& T6 L( A0 Q6 y, { . c* h( C' z' `8 Y5 \7 Upublic long longValue() {! Z/ W( [3 V4 F9 R' W1 V& e
return (long)get();" K5 W% x q3 z
}3 Q8 G! [& M* ~
) g, V2 c6 j3 b" G7 }