AtomicInteger类 ' A) a! j- L. T9 L) |* x ) \! ~4 g: h! g0 g: J9 H# V8 y7 O这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 " C8 Z) |3 E: J9 H3 I+ h+ v, [6 x2 O4 x2 F3 p+ w
重点内容8 l; h" Z8 G9 }/ K
& P4 Q r2 p& Q' f) ^7 V+ n属性: * ] k! W8 B2 V: n+ `" u4 fprivate static final Unsafe unsafe = Unsafe.getUnsafe(); \/ V# U S* q; E2 G p3 t" U0 N1 Q4 S5 x% Q5 m
private static final long valueOffset; # j8 n' D0 N/ B0 w( X 4 H2 z" E) P; V# Gprivate volatile int value; % H. {8 _& x& R2 N4 ?0 ^2 y# f ^1 h* N
静态代码块: . o/ R! [' r) V' n2 s0 kstatic {1 K6 ]1 ]) j3 p
try {5 I' }/ A) z& S# q) K" T
valueOffset = unsafe.objectFieldOffset # V7 _/ l9 x; @7 N4 `8 T2 C0 O; c (AtomicInteger.class.getDeclaredField("value")); $ A: C4 f% _: L: R4 s0 t } catch (Exception ex) { throw new Error(ex); }" J R @$ i, b$ h+ V
} % F$ S' k9 M) }- j) k* B--------------------- 8 K; s% r' Q& \. g* \9 ?构造方法 ' O) H/ {+ R) |# Q: W( N! spublic AtomicInteger() { 1 u* S# C4 o% j4 B8 C2 \ }7 [1 J; ]" A& l" U6 R1 k( c: L6 R
0 p9 U0 p$ s$ v* k- Z
public AtomicInteger(int initialValue) { 8 g7 p) W. ^" w; W# r value = initialValue; . V' b9 n5 Q7 v) ^! w: E } ! c9 j' _" q* J: u2 h; m- ?( |7 |9 @$ O/ W8 a' j; U8 f' `6 D
对象方法: }. a i d' s4 v; k public final int get() { ) E6 C+ X7 }# f# A return value;9 K5 [4 l5 t: y5 ^4 s% f* x
}1 s% v4 P' a: ]7 c5 T
2 S# j4 f* D9 E6 e1 `% D# T0 G9 y% a. d/ x% H: M8 R6 [$ [+ {& Q
public final void set(int newValue) {. t, ^# U; z* l! H: H
value = newValue;; ?$ s8 N* S/ f0 S3 O9 h
} 9 A) G' T0 J4 r7 P' N3 N T' U : z/ M* K8 [4 A; q# {# D5 ~3 z5 @( I' B$ L# W( Q0 }% `9 s
public final void lazySet(int newValue) {3 ^2 R" S$ H6 D! m
unsafe.putOrderedInt(this, valueOffset, newValue); 5 v: b& n% t: o! E }7 V4 K1 L# L1 Z: G" q$ ?
0 l1 a& D2 ~' _% v# O
( K* e: L8 ^3 Ypublic final int getAndSet(int newValue) { % f' O( q' ^" a: W; J# p# _ for (;;) { 8 l) `# m C' B+ {, k- s2 x int current = get();0 Q- e" P+ E: X7 ?
if (compareAndSet(current, newValue))4 o/ Q+ E% |$ p r4 c
return current;3 `5 U' F/ Y$ N8 \5 }
} " i& z) ^4 V3 X: t# h } : [$ V% G9 o. d7 s3 @; W& C( d- k. d0 J$ R, c& m5 N
+ o1 g1 d$ e N+ Spublic final boolean compareAndSet(int expect, int update) {6 M/ ?# i) A" K
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 2 d% |: B( Z: r0 C' e$ O- p2 t }6 C k" \* L% S/ O9 I3 `
6 `' n) ]6 [1 _) t # C0 T, p! O& @# I/ Npublic final boolean weakCompareAndSet(int expect, int update) {5 k7 X C: A0 Q( j
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 2 j c4 v$ _1 E* G+ R* S, S } ( q" `! e, N2 {! v$ r2 C6 I- Z & H% z% b' h3 _# Q/ B: m. Mpublic final int getAndIncrement() {* `% I- I% K& j, W- \
for (;;) { 0 B7 Z$ }1 g$ g- z) Q) M7 ?0 k% b int current = get(); , m; O4 [0 H3 q" a7 ^" o int next = current + 1; % T e3 z# A8 d if (compareAndSet(current, next))% c/ t+ p9 k$ r$ _$ O
return current;( V* @) N' T4 u% G" A5 r3 X: }
}5 y! X% A' A' u. R! H
}+ E6 R. [2 P: @
$ C8 k; |/ h w9 C. ?
public final int getAndDecrement() {+ g% I! s; G6 U: D
for (;;) {4 a+ d' y( W: O1 ^. o
int current = get();! c R& V3 B# L* `
int next = current - 1;2 {. L: T8 e% D
if (compareAndSet(current, next)) M( l f* a; ^
return current; & a1 N, ?' d& U% c y# ^ } ; W: A4 R L& X" i& E, k6 r' c6 a } ! H! A. j' u! W g! q9 s$ x 2 a! X6 P0 }# N4 ~$ ` ; j) c- N% N$ c: E" D' K1 @public final int getAndAdd(int delta) { - a* N$ o3 P: z, @! A9 N for (;;) {, H. k m* T/ e0 q" }' k
int current = get();9 Z6 a" m1 W% t l, R
int next = current + delta;9 S, \' F6 L, h/ k$ A; L
if (compareAndSet(current, next))3 y8 g' R$ U/ I3 C1 F
return current;; R' G. d; w* c1 M
} # v( e- \3 G# J) ?& k5 A }5 C- E1 V" ?0 `8 a6 g
1 v0 @5 \8 b9 W9 ~5 E* H' l( N1 @; n$ D/ @5 ]$ \8 Q% V
public final int incrementAndGet() {* s$ w" `& q$ o3 v2 x |
for (;;) { # H" e0 ? d( g( s/ X7 E int current = get();" G: k' k. \- x6 h0 t! W
int next = current + 1; % ?( \) r9 O4 ]: b if (compareAndSet(current, next))' ~8 X1 \# t0 _( ~) Q @9 S
return next;0 G$ d- c5 c+ \ y5 ^: F" a
}3 R* }# ~1 ?/ ]9 q5 r: R7 A
} 0 L3 |5 p2 t1 R3 N7 _7 P( ], [6 O1 g3 c5 L
' L s5 d) m/ W( g6 R- V7 R: E
public final int decrementAndGet() { ! J, f/ |) v8 }" h, O: x7 X/ O for (;;) { 5 v) [' ~, c4 @, D0 R int current = get();% b1 u; J1 O# a3 V
int next = current - 1;. I6 i( k6 p8 \ I- i3 i" Z1 O
if (compareAndSet(current, next))$ |' S/ X' C: U
return next; 2 l# P% m7 u" w }+ y( r0 ~% |8 Y; H. E) I
}& V( Y2 D- s4 D6 d8 x0 `! i
3 y3 Q5 C- W1 `) W
5 w: J$ d' _# q+ A! i
public final int addAndGet(int delta) {4 E% Q' f! J, z1 I
for (;;) {2 k1 J$ r" c) K! }1 P2 T
int current = get();3 p0 A. e& ~; o% s
int next = current + delta; $ J' H* J8 b3 \! q if (compareAndSet(current, next))* H8 V, }0 p5 T0 F
return next;, j- o% u: ]9 A& S, \% f& \
} 9 A4 V* C8 C# M Q9 W. R3 m6 ~/ _5 e } : w- n) s0 E, R4 Q6 c8 G 2 V/ B' D6 D% Y" X7 Y% T" U* \$ B9 X& B( G" S
public String toString() {- U) d' }( q% }$ c1 |4 R& Y) E. O
return Integer.toString(get());; k! G/ @- o, o' c: m. g+ g1 ?
}) o* M/ ?1 b3 q5 S! W
/ G+ W1 E2 C% K6 i/ t
5 Q* i- K- y& G1 g# Dpublic int intValue() {- L' g% m2 Z/ y0 D+ ^! y/ p: e4 c" G
return get();: A2 J, Y) ^& d1 M* C
}6 y9 r$ m# E( o3 x1 ?" N
4 U, F- \# K' B" j