; ~: ?. q5 t! ]重点内容 " k' i: c* r) X% ` ^4 U0 t2 @9 _0 Y, c
属性:4 D% v; T7 K+ b$ }7 }
private static final Unsafe unsafe = Unsafe.getUnsafe(); 2 o9 x5 b; f/ r! u% @# ]9 D ( ~% K3 M) [. Y' l. M; R$ ?private static final long valueOffset; 5 T1 t( V5 ~% x" O! d! p. J+ l6 Q) ^; }; L f
private volatile int value;$ N$ j5 ~- ^! d. G
2 z8 j3 U! \/ d+ t; K3 `$ T7 D
静态代码块:) e% m+ r0 Y; l7 F5 X& z& t6 E
static { 6 M4 G$ O+ m1 R try { . @* I( T, `% L& e: T1 R( @" S valueOffset = unsafe.objectFieldOffset# h5 [# o! N4 P/ h* E# l
(AtomicInteger.class.getDeclaredField("value"));2 N+ {1 v" d4 ?/ U9 W
} catch (Exception ex) { throw new Error(ex); } 6 S D4 ~5 e" E) K3 I } : W. [6 h" ?6 u8 l' Y/ t: O--------------------- 6 A( l% N% t; b构造方法) d$ [; P* Y5 A; c/ l
public AtomicInteger() { ! A( L7 s& w: q }- c! D9 w$ k( v- u" _
: @! p1 a5 v P( T0 `3 t
public AtomicInteger(int initialValue) {9 _! `. ^. m L7 Y
value = initialValue;# o: V( a% s# @ F
} 5 ~- v( l ~- l" U+ H - m) r4 z* O6 Q4 J对象方法: " r& \/ j4 D- @$ | ^7 d9 a public final int get() {+ Y# i7 v @9 L( I2 J4 Z. O
return value;5 B( J4 h: g* O+ O8 G
}5 [2 }$ t N( W! n
5 E7 ~. T* q7 B$ G8 U: t6 O) h! R( r5 b( ~0 S: V3 m/ r' K
public final void set(int newValue) { , D2 q! H5 j7 E7 ?$ Y( N" I9 v value = newValue;; [9 ~: L9 G- {( R3 E8 C" V% {
}" D' X, _ ~. B/ V1 M7 H2 h
1 F0 w4 J& [# m8 S
/ v: A1 z& C4 }
public final void lazySet(int newValue) {& k/ E/ u0 I. L: Y3 T5 T
unsafe.putOrderedInt(this, valueOffset, newValue);4 ]9 d7 U: w5 S: T
} % T" A: ]% x4 S8 l- e5 n: {" M( V6 @ S* v
# R6 U3 [4 ~: ~" ^3 e% T
public final int getAndSet(int newValue) { g' P! J5 [5 X- G) l" g0 _
for (;;) { " D5 M, B5 s: f0 t1 c5 [( i int current = get(); ) {* v* k2 F# Z8 w if (compareAndSet(current, newValue))9 F1 `: D, P6 c& u
return current; $ ]( R/ E) j- l- N) ` }2 d, \. X) o# Q* B
}9 G' \) g4 M0 s) T8 e8 F$ f
" m& @- o, b' H! m& ]3 H% G. p
* o) E* Z. M. K: S. ~5 s9 _1 m% s
public final boolean compareAndSet(int expect, int update) { ) W9 B, j% c& M* b1 v return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ) N5 F: l: l% n9 \2 x- V, i } ' c# F& r. k& D& _2 U7 ` 8 I$ P1 M7 z0 B0 A* P 7 p8 `- Z! M9 e# J9 vpublic final boolean weakCompareAndSet(int expect, int update) { : K1 {1 z6 W% `- ^+ c( O# `, M* I( | return unsafe.compareAndSwapInt(this, valueOffset, expect, update); , o# ?9 V; @) u6 J k$ c' t, _ } . l7 `* n& d5 w0 v$ Q5 k/ g4 o3 {1 _3 s' u& H1 Y1 c
public final int getAndIncrement() { " l8 C' ] n5 I( g for (;;) {! V2 H3 p" x/ w+ r# S+ \* h" L- h
int current = get();7 l. Y/ a2 A5 d* M
int next = current + 1; . j" X. D8 v% z9 O! [3 } if (compareAndSet(current, next)) 6 f T9 F7 |- D" r% @$ i return current; . j) t1 w$ P4 i* l( O+ _ }; ~2 E4 D7 U2 x7 A* _
}" D3 Q: t) ~, u0 A& z
, ~! y& e0 H0 g, ^0 L% u
public final int getAndDecrement() { ! H+ P& H9 f9 H! @! D' | for (;;) {+ y1 Y# }( k) @0 L
int current = get(); ' ]1 ]; ?; Z D+ v9 A int next = current - 1; " Y+ ?- K9 |, g/ |3 N3 ~9 E( `. o if (compareAndSet(current, next))9 N. B# j$ K2 A3 A$ c
return current;3 G1 ~$ T# E; }' u/ I
} % \, C, |: l; a/ w( G$ ~8 W) ? } , n+ [( d# m* A. ]) s3 n5 X! B - n d! u3 N6 Q7 v7 [0 S 6 B- G r* o" x& x7 Y% q) o* e& _public final int getAndAdd(int delta) { ) y' ]0 @ H0 d for (;;) {! \1 y- ]. `, d% F9 p
int current = get();; a' C+ V! T% U2 S, S" X7 h
int next = current + delta;3 g+ P) p/ A( ^7 H$ w. |$ j5 [' i/ Y
if (compareAndSet(current, next)), q" \/ x, O% e" |4 j7 L
return current;( \9 H& s4 X) u7 _
} ' ?* v( L3 b' p. `& Z }/ s6 A' c" \5 t! x u7 P, d( d8 n: `8 m
" F4 l8 v6 Y' l7 E. o, J
& ^1 M8 Z# u0 f. c; h$ U, `
public final int incrementAndGet() { , H7 f- p8 z- C8 I for (;;) { / s" C8 F2 z% o5 A, v int current = get(); 8 O$ a' }- p, a' ^# ] int next = current + 1; 8 |& A$ a$ ^* N9 D0 z if (compareAndSet(current, next))! e& d0 `3 q+ |/ j1 b5 ^
return next;+ x9 j! d3 I+ M+ `; m
} " }! z5 T: R4 A8 v }4 B* G& v) Y4 u
# E' y. f0 }( j, } ! p2 N% N9 E5 w! F- Epublic final int decrementAndGet() { 0 b* z0 V3 O! X% i for (;;) {$ N. R2 O3 P( j8 R. n3 r
int current = get(); - \" p" C: m0 `) x. i% ]+ X' E int next = current - 1;: w; C8 U0 z% ~/ b* F
if (compareAndSet(current, next))# g- L' E0 @" Q
return next;$ Z+ _5 n/ m) W" t. s4 c6 D$ k# A
} % Y) t: v; L; }4 P6 U }: t9 C) t+ ^8 e$ E
8 C% k* r' J6 N 3 T u. ~ W3 n, ], j! y% npublic final int addAndGet(int delta) {' m" ^8 ^0 r$ b; l+ D* b
for (;;) {& ?6 g9 Z/ l/ Y6 x1 B$ W0 |% { q
int current = get(); 6 D4 i/ S/ `/ F, A3 w int next = current + delta; 7 V4 A+ ?) Z" r' H7 O6 [ if (compareAndSet(current, next)) ! H8 Y5 W5 \. x. y2 n return next;( Z7 j* G4 o0 c, N2 n/ c3 \
} 2 n* h; c' q/ G* w* N( ~! O } " w9 R6 Y# J6 g: N 5 z. ` C7 Y- f# F0 O7 D) P, T& V5 @3 A7 F9 g/ B) v! Y% b9 G
public String toString() { # ^1 Z: Q) }6 d! R+ e e! I return Integer.toString(get()); 9 u, Z/ Y( H* |" I( C }9 g. O0 q2 ~" \& ]+ k2 Y0 z