6 ]0 w! _" _6 e E9 W这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 % ~* R! H' r# r 9 d- z+ k" H. @, V9 z重点内容 # F' G# J0 ?7 b k/ L1 C% `6 D; G" E/ F1 D' l/ R) E5 @& E5 Z
属性:8 J" s( p/ [1 L& p) H$ Y9 V- N
private static final Unsafe unsafe = Unsafe.getUnsafe(); 2 L7 x2 |. P U4 X+ s1 i9 w9 Q- O: O$ m$ V& i
private static final long valueOffset; 4 K5 g* \1 U3 q: c/ h' m" Y5 F2 s# r
private volatile int value;+ f N6 e$ T9 Q+ M5 `
. @& w7 M" o) L. x0 C7 O1 P
静态代码块: . F3 Y; x; z& o0 Rstatic { 5 y% L+ Z2 H, T$ q" W4 h) b, b try {+ u# s, T+ e' I( [$ _/ b( C& p
valueOffset = unsafe.objectFieldOffset - _5 ~: e5 R3 K, b; N. I (AtomicInteger.class.getDeclaredField("value"));/ Y, h7 w$ ?* w+ |1 _
} catch (Exception ex) { throw new Error(ex); } & Y% O( l& V) P- d3 `( H }( Y/ v; {6 z5 }9 U U+ o, B6 w5 R2 k/ A
--------------------- 1 H) S$ w- Q9 E& _: [% f0 Z& O; @
构造方法 ; W2 R2 b) A) K' d: }& P4 f* |: Q$ Rpublic AtomicInteger() { 1 y0 y& R+ U1 C7 ~" u: T4 I } & h* f3 `6 I. V1 v1 k+ I7 X6 s5 N$ g9 L
public AtomicInteger(int initialValue) { 8 C# E, G5 n% j$ O value = initialValue; 2 X3 A& G% C4 @ s' v }9 g% w. O) F3 J2 J C
$ q3 |: y( `' a% ~0 q; D# v# Y; o! C对象方法: $ P& L6 `2 s/ G public final int get() { 3 X. L- c8 m/ t- A# y return value;7 {+ x7 P* h: U0 x4 D3 M" w) d0 t
} r4 H2 I. ^3 w5 s; T6 L# G
* v) Z& N9 `/ ^
+ @8 F$ S" r! u9 R/ C: |* e9 xpublic final void set(int newValue) { # i6 m# F' K i value = newValue; / ^- @% V) U3 U! h } 8 h" u+ ^+ b- E# I/ y2 p# r# C+ B : S* r4 `4 _# }! ?6 ^1 B! L8 r2 I1 b8 M
public final void lazySet(int newValue) {3 D6 Y/ \( z# b" h! f- `& M
unsafe.putOrderedInt(this, valueOffset, newValue);* P- H- Y1 `- p9 B# w! N; U# h
} & y. {6 W f& |- c/ E" b0 R. A
1 u9 T0 _1 u* N* N* h* z$ H7 g
public final int getAndSet(int newValue) { 4 o# s" l2 K% g, M4 u for (;;) {+ m. F& j6 u4 E$ u6 x
int current = get();" i" W. m5 N( ]& E
if (compareAndSet(current, newValue)) V- h, m+ D- z1 v2 W; } return current; 1 q& z- D" y. V u x }! W' T. F( u6 V+ @/ |# x
}( {2 m1 S1 m8 o* u* u# r+ J. I
: J3 X2 ?, \: w5 Q% O6 D% O+ K# q ?8 N8 G2 C1 I' d
public final boolean compareAndSet(int expect, int update) {: f4 a0 y! ^7 Y: u# F$ @7 W
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); - v2 \4 @$ @) y- i( N6 L } 2 _; a! k0 B, b2 u 8 W* e7 o/ x8 ~: K7 r2 e 3 O/ K9 c( I9 n' E! c# S+ m1 b2 d: wpublic final boolean weakCompareAndSet(int expect, int update) {+ a" g" f# R M
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 6 [5 P) k* p4 F( x( H" K; N } # V6 f# Q! ^8 l* [( C. o! k; e3 z4 e9 c- T5 I6 l4 ?# ^8 |
public final int getAndIncrement() {9 p/ |1 M6 x- Y# }1 {
for (;;) {$ S+ Y: J# R4 a9 g
int current = get(); * h9 y p; w/ m0 C int next = current + 1;$ K1 @( E$ q; @5 J2 f: F3 `7 ]
if (compareAndSet(current, next)) ' C7 L; b2 w1 y7 H! V" w1 W return current; - u" N% [) B% [/ j6 |5 u } R2 I8 H/ ?, {7 @0 f }! G! U7 l# S3 F5 m* ?1 }# y/ A
% O D% d' l% H6 N6 P2 x1 I$ }; Ypublic final int getAndDecrement() {1 l' A+ W R& g# t9 M& q: n8 E; P! v
for (;;) {; h, [0 N) M+ ?; N: z$ d8 x
int current = get();* V- S+ R. Q X
int next = current - 1; $ I$ @, r9 ^3 Y* f# L0 Z; T+ | if (compareAndSet(current, next)) & ^. q- l4 g. l- } return current; & K+ ~. E- x% ?. f0 m } " R2 S6 u/ L5 N/ \" d' ^. C } 3 C5 K9 b& Y+ `2 ]2 M 7 C# \) e# D. e: D- z% |- Q+ r! x6 [) w9 a4 a* I$ G7 I
public final int getAndAdd(int delta) { % o* z0 _& M& h9 F. [ for (;;) { 6 h" s9 F, ~+ f u, Z int current = get(); . W8 {- I9 ?6 r int next = current + delta; : Q% a# @5 I- W7 [) J if (compareAndSet(current, next)) : S( s8 @. [6 x3 l/ ` return current;" X) y) f2 z4 \5 A6 F
} % w% s d+ i, z9 H* E: Q: { }# g8 J7 h# b/ g+ x( ^
\1 d1 R- A8 K9 A) i( c( g- q
8 I$ \( w2 K; S8 _% h$ n
public final int incrementAndGet() { , K9 K: S4 s; C for (;;) {8 v, i a- g- }2 a* a7 k
int current = get(); % T! Q/ z8 L& [6 R) U( H int next = current + 1; * T C- d8 l" B) U' o8 u! U2 F if (compareAndSet(current, next)) ) H, k' L5 F. y9 B7 W return next;! D+ Q3 g+ ?9 {. s
} ( y; w2 M( V% z) H/ _ } 5 R6 M+ T [, C/ J" |; _- \7 A. _9 u6 Q/ R, e
4 y& o! t& q$ k1 Y! S+ Hpublic final int decrementAndGet() { ) m# n( f2 `# n1 K" g for (;;) {% F+ c) v0 R& {! Z4 L7 X5 E+ d h
int current = get();( V1 Y+ k; Y( H! L# b% c$ b
int next = current - 1; " X6 N4 S- O! t3 C3 m if (compareAndSet(current, next))* o$ G; h+ R- G/ g: [& E
return next; j3 p8 m, ?3 _9 C, a) a } 3 g- u# \# h6 z4 K( L* F( ]; M } , j5 O% I1 H# K+ i( [2 u ' o7 s( z/ s. V; F * [4 J- b$ u3 U, A% jpublic final int addAndGet(int delta) { $ H* m0 V W* k- R* a for (;;) {& H7 [* K/ L; F0 @1 N6 g* c
int current = get(); 1 c% ` M* |+ R; f int next = current + delta; 4 ]3 o1 s9 r8 J' J if (compareAndSet(current, next))0 @0 m. {. e8 e; {) |
return next;4 c `2 r; Q7 g4 P3 h2 i6 ?% W! T. e
}8 ^, }$ |: n: k" g8 h9 `
}) z9 V/ L( Z% c+ f! |# O" b
* y! b1 n& i E3 g! T! D* l2 y2 [& M% B* t# i! @, @0 w
public String toString() {' u, E3 @6 b0 }4 W8 z J
return Integer.toString(get());; [( G6 a7 k& L# r6 G7 C0 T7 d, Q
}) e. {; S, }' H1 ^7 W0 `, d
( L2 k! k7 U; f( E0 K
: e2 L+ j, y5 U" Ipublic int intValue() { O% x5 P! x* C! |/ w, n% O return get(); ! S) T+ C& u# v l0 q. u( J# \ }! A7 S6 D3 W' w1 d/ G. L) `. H
. J7 g- Y+ ?& V- M1 f
8 w2 f3 G, C; w! [; `' F9 q
public long longValue() { 8 m: o4 f% m& @/ U2 c$ Y5 \$ { return (long)get();1 z, ]* `$ j7 p2 q# _( a
}5 T! R' ^. f2 A! ?( u p
- _( H! J _( c2 a