5 w/ l! e) A. ?" h8 B+ f+ a这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。% D8 f0 C. G S, D
9 ~4 f5 X1 u/ r7 K N- k重点内容 " [7 [. W( X+ n$ Y" I7 o1 Z6 {) u w) n% \- R$ ?9 f: y5 m$ f属性:1 Z7 R- w5 U" x" u+ g# j
private static final Unsafe unsafe = Unsafe.getUnsafe();9 d2 N5 U0 G# y/ r; h/ c
3 Y. {% W' l! ~, h7 E7 c
private static final long valueOffset;" r8 n. C3 Y4 b" W2 M+ B7 V8 \
- ^2 b- C! R9 W& u
private volatile int value;8 q1 [& S* D/ \ O
1 D9 \' t0 \% A& w. H静态代码块:/ ? y1 f2 J( x! m) y
static {3 W+ w& U7 Z# E
try { * u7 a" H" `4 i/ u" Z$ W" E4 K( l valueOffset = unsafe.objectFieldOffset : I8 Q# N" R: ?# t1 i: ? (AtomicInteger.class.getDeclaredField("value")); 7 J; Z5 S' I b! [$ N4 m } catch (Exception ex) { throw new Error(ex); }# J: [. `9 W4 X! y( ~ [
} 3 d$ n" Q' u( }1 Y# j+ ?/ o--------------------- 7 t# ~1 b' L) P+ w. z8 s \构造方法 " C0 C( w: L. A$ N- t2 lpublic AtomicInteger() { % U4 b D5 t5 }& Q4 I }: P6 ], n. T! c
% G& D- F) l" p8 x) e
public AtomicInteger(int initialValue) {* ]5 |' n* c3 \; p- `
value = initialValue;6 P _7 b+ X! ~ Y
} ( b4 R1 S6 ^0 |& ^% T + u. g3 b+ _8 S对象方法: ) G3 x9 j: J# g# a: [# s public final int get() {6 O, @8 w3 L) F! |# H$ @' s" l4 d
return value;) M- h4 | l$ o' H! t
} 1 x% H; G& l9 q3 D2 M! S- v7 ]+ T1 P. g! K
1 i9 B5 Z1 T: u8 t1 }" v1 Bpublic final void set(int newValue) {* D* q! F6 U- F0 D4 r2 ?
value = newValue; ( c! A" i# @! {/ s$ ^8 q3 P. U/ F }3 f0 b" P, U6 {/ _2 M! d- p
+ G2 V- C% D8 P. G [0 t4 `4 B0 t3 ]# H# q& Y# k
public final void lazySet(int newValue) {& [4 }7 u7 l7 n8 b% P- _ C) O6 m
unsafe.putOrderedInt(this, valueOffset, newValue); ' ]9 [6 [+ t! V7 u$ N* ? } ( E9 {) P- @" ` U7 ^5 R: @ ~( m8 E" g& k* k& P2 M
+ }3 q7 i$ o2 {% ~# j7 }public final int getAndSet(int newValue) { , h0 e$ {! T* F! @8 g for (;;) { 6 P& W6 l) g' \: [ int current = get(); . p8 N" I" G- y6 _5 l if (compareAndSet(current, newValue))- I4 m+ `+ d h O! j
return current; 5 P, y1 V: N/ n6 C/ ~( j( R } ! y/ Y; F9 N$ X1 c H8 K( s$ k }3 S& z8 i( Z7 o9 }- q$ m% \0 Z
/ p, o/ g! D; |3 b 5 x% r+ {, t7 l+ s. |public final boolean compareAndSet(int expect, int update) { 2 I. `. _9 V) \3 A9 s9 d- e return unsafe.compareAndSwapInt(this, valueOffset, expect, update);. Y9 i; N& T% |* N5 W2 K1 I) {5 K
}0 M! O$ b2 J7 D& ~
# U( W+ ?* b3 f! k
% }5 P4 |2 i( z: opublic final boolean weakCompareAndSet(int expect, int update) {* I6 {7 t) S) O
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 0 U* i2 N$ v7 Q } ) ?* ^% r5 I! m9 i9 X9 t$ I, k& C, s2 g. y& E
public final int getAndIncrement() { ) A& M+ s' V, u: X0 y for (;;) {: L7 J. g# Z* B, j2 Z
int current = get(); . Z$ V: j, k! C5 e$ l3 u0 A int next = current + 1;3 t$ ?" M. R2 m* o( f' @9 W
if (compareAndSet(current, next)) ' C w' g: m" _0 g6 Z& m, q& | return current;7 o7 F u$ ^ T+ X
}. C# T# r* E3 |; F5 ?- P
} ' ]# o( y: ^8 h& M. r5 u & C w5 H1 ^9 I6 K$ K3 C% m" H! _* Wpublic final int getAndDecrement() { 7 Q! |! c$ W" `, J O3 T for (;;) {( t6 S. r0 E9 s, E$ V+ i
int current = get();5 K5 a- b+ O k- c2 ?
int next = current - 1; U* p4 L7 Q% k& L: {+ Z/ s: ^ if (compareAndSet(current, next)) $ t \! d1 u1 D" }& J* T/ h( N return current;, v! Y& G! s; [* ]+ F4 O
} ( n5 q7 i" c; ^, F) W5 [" { x }1 m5 ^ d2 m& @, x% x& L
& r$ l5 o* F3 M0 A3 ?7 z/ Y: X6 y) Y& }( a! T2 n6 p
public final int getAndAdd(int delta) { ! m' {/ e4 }0 |' u for (;;) {; Z7 x6 e8 u0 D3 v/ w" h
int current = get();5 |4 N" j( s1 |, j& f; U
int next = current + delta;/ W) c4 d0 J7 j' ^, o- F+ q
if (compareAndSet(current, next))) n. |" [( t/ B+ @+ f8 E3 w
return current;$ ~4 L$ G$ M9 }/ B# T
} 3 J9 B) V& y% n# b* G/ n }# w1 G: K6 i) P
, t/ j% D/ Y: l, g
3 V" t0 K+ h: J
public final int incrementAndGet() { # @8 S( H, P2 {( {4 _8 V% z% X for (;;) { 9 \/ I, s; n2 }% S. c; Y int current = get(); & Z4 I6 g# y7 O- v int next = current + 1;' j8 g! {) s8 f! K9 b
if (compareAndSet(current, next)) 9 \; X' B1 J, ]* T8 K6 s/ N+ X8 w7 } return next;' H4 ?1 Y) i' X; h6 |& e
} ) H- s0 B$ C& S; c$ Q }8 x* V/ D& k Y6 e2 e
. Q( t8 @1 Q$ f) g
3 }9 s8 c2 U" L* s
public final int decrementAndGet() {, ]' l; R9 X Z- ^! B! j. M
for (;;) { . t% Z& [3 U. E0 q4 @ int current = get();+ n, F4 m$ L# Z% B1 r7 D& l9 Q
int next = current - 1; ) l5 R$ J1 n5 ?: l if (compareAndSet(current, next))5 H2 M0 }2 P5 c0 j) p" M4 e
return next; 1 A! v( U' r6 v8 h4 r8 |6 I }* @3 L8 h2 i! h4 F- ?" T
} ) M+ E' R/ {- ] 4 P U5 |) O U& Y9 _. ^6 o( i& N6 z5 n( ~& c+ s/ V
public final int addAndGet(int delta) { 9 a q. A0 n3 d v+ j% y& @9 x for (;;) { ( s. G; a* I' |" w% F, { int current = get(); - o P* F9 i' b3 A5 v: C* n* B$ I int next = current + delta;9 y% d. x/ s. @. D. w7 v
if (compareAndSet(current, next))0 w) j" ~/ V5 T" a) e
return next;4 L$ I2 q5 r6 k) a1 T
}0 o, u, a7 A4 F! x
}/ a5 W/ E M* [5 o3 [4 v
; R) E* c% `# Z1 l/ s" {3 U: Q6 j1 n; m0 I4 v+ G7 `8 w/ p# N; E6 T
public String toString() { 7 s1 X7 `* V7 p/ n' {9 U2 K! `0 _ return Integer.toString(get());& ^+ O! K% P. }) C/ g
}& l* }; U. b. l) P/ ? p
' `8 |7 F- X2 c9 k2 G0 h& [
/ K; x/ F/ S9 U% K; ^9 W
public int intValue() {; H) U3 z0 W+ s; J1 ]8 L/ Q0 }
return get();$ n+ s* t6 X/ u+ e" E: T; F
} }4 ]* d: _# h3 |: X+ j
) }4 ]6 E$ w. ~" p* |
' F9 e$ V b4 {* z+ V. H5 N0 ypublic long longValue() { / l2 _) V: v* a) p3 ]9 ^ return (long)get();1 M7 v9 G, N8 ~+ Q1 o
}/ V' v5 Z5 ^# q+ s: S6 t
) v+ {5 e& S% ~2 ~, Z
/ s* V0 d+ q% o% c4 z& Lpublic float floatValue() {1 H/ g5 f3 t! l( l/ j- s0 m3 T
return (float)get(); 0 t9 H# n- C' E T9 U# w } 0 C) E# W4 D( V @ u q' p8 L/ P) q