AtomicInteger类 ) u B) n8 ?5 C+ V h! W Y3 y ; s/ Y- y2 `; U5 e' f: ^: A这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 9 i. ]; g+ W6 Z/ b6 Y# E4 T0 @+ o
重点内容) V) x& K$ l3 k4 `, y
8 l! a. G0 \- P7 s5 Q
属性: 0 M( Q" q# b$ d( e% G# `private static final Unsafe unsafe = Unsafe.getUnsafe(); P9 a' [4 L/ ~% v/ c
H+ ~( l% ~; C" v% f) N+ iprivate static final long valueOffset; $ e8 J% C1 q! N1 U( t, k# L3 a % z! E) k+ l5 [1 [4 Q0 ?4 Wprivate volatile int value;& S0 o: b+ U% H0 |
# u5 ~9 @0 H7 J7 N: Q* ?: _
静态代码块: ' L, S# q1 ?# G! Q4 ]+ tstatic { 4 D9 L' F! D0 N5 Q try {) G& T0 }0 D& }5 G/ l
valueOffset = unsafe.objectFieldOffset 1 N" W; I& q% {5 | (AtomicInteger.class.getDeclaredField("value")); 2 H! B0 V1 |8 J } catch (Exception ex) { throw new Error(ex); } % M* n2 g6 `6 o9 o5 f8 }! Y } 1 q- @" d( y* l--------------------- / L9 k3 l$ _- v) h" ^6 N. C
构造方法 " T, H6 P# W% `public AtomicInteger() { f8 I, V" {$ q5 t
}* C6 ~! e! p3 v, V. l6 K' r! d
+ l' J8 w, ^. S- ?1 p& l
public AtomicInteger(int initialValue) { ! l) W) a& ]8 l1 Q value = initialValue; ) I% Q, l& W1 f6 t3 g } 3 Q& B, v, U) m7 i/ a" S; m' K + S- D1 N# @( \ ~- c: M2 d1 a# c5 |对象方法:) J9 H1 J$ v9 I/ F
public final int get() {. @+ L2 |3 h, Q0 J/ t2 ^$ ~
return value; - b! J. d9 X _* Z- ?! O2 T2 T% Z) m- x }6 i e( ^1 W- t2 w( n! b" ^' z
9 |3 q4 V8 |- |# t9 I! w3 P/ g! O
5 e2 g- g8 Y( b) w
public final void set(int newValue) {( }. B: q/ I. N3 P8 Z2 F2 Z" j& T A1 v
value = newValue; ' @9 v( T) _( }/ Y, b4 s5 B& m* t }: x* F" s- k6 m" y7 T6 L
- L6 u. Q( B9 d( |, x6 T 5 B" B# ]" u- n; Wpublic final void lazySet(int newValue) {! x [. b7 @: j- F E |
unsafe.putOrderedInt(this, valueOffset, newValue);+ r1 r8 c: J( X4 U% a O' ^7 e
}# g4 }9 E. t. B. W
. R7 y* W+ @* }+ s
/ q0 _2 c+ ~* s
public final int getAndSet(int newValue) { ( o; n& z L+ w$ x for (;;) { 6 p1 r/ B, ? d8 @$ d: q4 m% n8 L3 i int current = get();/ _ @. h1 l" k$ P- y6 X( c$ r
if (compareAndSet(current, newValue))1 H) Z5 R7 S4 z% [: d2 E6 {5 f0 K
return current;0 Y( K8 Q4 y* O! z3 `, e4 w5 y2 r& E
}/ p# u H' u! u' I3 r
}6 \1 q/ ^2 |: Q; B$ Y$ a& S) H+ ?
6 ]. ~8 V; U! O
& {+ E* `3 w! U+ i9 s$ q, q
public final boolean compareAndSet(int expect, int update) {' Z x) k; {' _3 ^
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 5 P( T: G* n7 V1 g6 F M. | } . L. z( Z" k; R1 v9 h' }: J7 a# }& A( e1 l
! T& ~7 l7 J r( Y7 T% O7 l3 \( \+ W
public final boolean weakCompareAndSet(int expect, int update) { * J/ H) p; O7 _5 L4 }; B return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ! z/ H0 B/ z7 o: @6 E }" ^' L' i5 Z7 p5 T% E; b' ?
! E+ r$ e/ T( t' f. ?" |6 }! A# Hpublic final int getAndIncrement() { * m% l1 {. d5 A for (;;) {. e0 O# L* I. W/ D8 p0 ]
int current = get();3 D& V1 m2 M% \0 o% R- `* s
int next = current + 1;" I! r% e6 A1 e! ^
if (compareAndSet(current, next))$ M- T$ e5 Z. {
return current;+ L+ k9 n1 a/ D! M/ J
}3 i% u% D, i/ S4 y
}' \8 H/ }: B" p8 U6 F. |; Z1 I2 G
2 n) p% H) V _, C8 N+ T
public final int getAndDecrement() {1 {3 N4 ]7 o; T8 Q# s* h+ n
for (;;) {- N+ O5 N- I! x- t
int current = get();' {7 V# t! }) c: `+ o/ c1 k
int next = current - 1;6 G$ e v$ L8 w$ r, `
if (compareAndSet(current, next)) * s+ P7 W1 N8 b return current;+ o- w; d7 f8 R7 t3 a( @6 m& q
} ! l* Z7 M% ~" E2 j/ R; G } ; t" @$ v8 {- n. ` ; Y: c$ }0 l) t2 G, g/ i 8 {% x! V1 f, l, z. H. U6 \) ?; G6 spublic final int getAndAdd(int delta) { 2 Q% O v* X9 X: O- E for (;;) { $ }0 R+ G# Q+ q7 Y( q3 N int current = get(); ! s$ s o" M. E7 a/ ~% ~ int next = current + delta; 7 }% |* r9 O* O b7 }" _ if (compareAndSet(current, next)) , T6 I9 _+ J) Q8 l5 G6 q: G+ h5 `, k5 w return current;' R+ i }& o" w1 G+ ^
} $ K5 k" |3 y5 B# G, C }9 x2 R: `. g/ o/ h/ j, |
$ x- V* K" N0 Y: T: f, o
. ~& z6 x0 E0 b! P4 X: V6 ppublic final int incrementAndGet() { 4 G/ L& m1 i0 Q0 j9 i4 n* E for (;;) { 7 N* D* H7 [) s: l v int current = get();3 q$ G4 s8 u/ |/ q; Z) {
int next = current + 1; : s$ H/ C' k: k" z' a1 Y$ ] if (compareAndSet(current, next)) 0 J$ P! h( F5 E$ i+ L return next; ! a% |" I% U K' |0 B2 V0 U } 3 `- r# l4 {7 B) P } % E. T1 N; L5 k+ T/ K1 S 7 w* o0 k. S$ ?1 N+ F, j3 |% z- k; H6 a) S. Q
public final int decrementAndGet() { 5 ~. Z1 C3 J E* a L( j for (;;) {1 l& l6 @& p# u9 B+ \
int current = get(); * k. h8 ^: K8 C) _6 S int next = current - 1;; @' h6 v) t. k2 G# v4 M+ B
if (compareAndSet(current, next)) ! C' z, t; X8 a( ^; T# P% V& H return next; " k. e( R( \7 L8 q6 v } ) X8 P0 p" c9 L* F- ~, e' D, K( | } ! T" v2 Z# }* i- b3 o- O4 h+ [+ P& k
5 `* t3 N: ~: I+ xpublic final int addAndGet(int delta) { 5 ]1 m- Q+ A4 q for (;;) {1 U- i# C3 p2 P3 g
int current = get(); 2 E6 ?2 p9 q0 w2 U1 j int next = current + delta; 3 D5 F% N/ E* i if (compareAndSet(current, next)) ( I" S2 B2 o( h0 B$ {7 y) e return next; ! E: \9 p- i) n* t a0 I0 r% C2 X }5 a# u. t3 A5 M( N5 s% N
} $ N5 R6 Y8 g5 s' I0 L2 ]6 M - L, x4 m1 [: r: P + t& o; M' A; D4 hpublic String toString() { & T- A& g' {6 t; w1 X6 ~5 a return Integer.toString(get()); 3 R! | S6 x8 p: S0 F" B3 M* c } 2 H+ n4 y, R/ F- |& j! l" K 2 B; i* Q$ O6 Q6 _0 s! E, s7 l; d* m& n: l* _3 S
public int intValue() {* B6 f3 t1 v4 s7 y. v/ q
return get();: Q$ Y4 B2 p2 C9 T1 j2 o% H! e
}- O6 z0 }: F9 H! R+ |3 R
4 B- ^9 p# S3 ^$ O9 {$ c0 S' M) E' I# ^4 }7 w6 u: O
public long longValue() { ) u: C$ v8 c9 P1 Q, n4 `/ p return (long)get(); 1 Q! C e+ v2 ~( s6 {+ g }% v& N3 u! r6 M8 b x# N' K& T$ C
/ s2 t" ?5 \$ @8 |