AtomicInteger类 - ^% i9 S: X- p2 ^6 ^# w% _) \ % z/ c. N/ }5 [. Y这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。& O) q9 K& m. x0 m# _
4 D0 G4 P- y& O! ~
重点内容 3 L: B$ f) j- f# B # @* E* h9 K, H4 z1 k/ N2 C4 S) V属性: 2 h B' y! V4 [, }private static final Unsafe unsafe = Unsafe.getUnsafe();9 M; u( q/ j S
8 ?; Y+ G2 J' q
private static final long valueOffset;( j0 ?, y* M1 Q' P1 H9 p7 r
* W- w4 E9 K, p* |' _% oprivate volatile int value; ) W5 e3 {" F B) ^ 9 C) a( _% S/ B静态代码块:6 L: C& P, U& Y: p- u
static { / C" z2 i# K6 D4 m+ K0 { try { ; W! v* M' O; T" P# Z valueOffset = unsafe.objectFieldOffset. O/ i& G$ k$ a% n& I9 M9 b
(AtomicInteger.class.getDeclaredField("value"));! n- a T% a' } \; E+ ?# U; Y
} catch (Exception ex) { throw new Error(ex); }/ A2 @3 R h" S
} - P1 p' Q: w" i+ E--------------------- 9 ^ A2 e$ z/ ?4 `4 [- O6 B构造方法% T0 |) E, G- v& I% O
public AtomicInteger() {- e2 d8 N$ b& O" {0 O1 _1 o
}% x% q' j4 U9 e. B( z% j1 h
7 C) A+ g& h# vpublic AtomicInteger(int initialValue) {) U/ V6 `$ q' I$ I2 G t+ Y
value = initialValue; 9 e9 c9 d2 R. e: P* }1 J" E H } 7 d0 r. y: G0 R4 N3 s4 w* @0 h4 _& {2 H! I, q! p' t
对象方法: G8 \' M7 x& C; d, i9 C public final int get() {5 G) P3 v( @# @, p
return value; # B/ ~& A0 W+ g" F' s2 O }5 J- n- X; Z8 Y5 a
+ z! ?% ?8 A& {+ u9 W/ q" W / s0 w: d: Q" B& A3 g" e4 O! A+ B& Ypublic final void set(int newValue) { 5 Q; q4 e! w1 l2 z d value = newValue;4 {( H. M5 B, W% X" M+ K. [. ?
}' s4 l$ l5 r- E% `+ c2 u$ s
) M0 j# |& b/ E& _6 w' g
) h6 R4 X3 X0 R. T! c {- p
public final void lazySet(int newValue) {$ v0 r. Y9 Y$ @9 ]; F
unsafe.putOrderedInt(this, valueOffset, newValue);2 X/ l# Z7 }; s! S$ ]1 D
} 6 d4 }- D3 T3 D3 L. K9 B $ |9 a, k) I" t# n1 _% ? * N9 f% ^7 D6 jpublic final int getAndSet(int newValue) { 7 T# n i" c* M( m7 a for (;;) {! g- x( S( n- c1 _
int current = get(); ! U3 K4 ^! ?3 V5 I, T' K! e. Y& M if (compareAndSet(current, newValue)): W; }" R; O5 R. U, U
return current; . W: e6 J y+ R8 A& }! R7 I } ! V! U& l2 \3 N7 z( _4 n } ) P9 `) p. P* K: t% z) y; E- ?; K! O% ?! y
! t% K0 f/ B2 V- f2 ?4 n' v2 E
public final boolean compareAndSet(int expect, int update) {' M& O9 Z+ ^* O1 z) x
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); # M4 r! A; E: G5 G7 r/ g/ T }: ] r V9 N6 e. v+ j, x7 ^" O
, d3 \- O! G+ z/ i G( d- K& N
' N7 Q' j; o8 a. v1 Y5 }+ ]
public final boolean weakCompareAndSet(int expect, int update) { M3 h/ x, }; B' f5 S
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ; j3 d7 o: B' I1 |0 l; n3 E } $ J+ ^1 A& v& ^3 k* z) D" s) ?/ I- [% [3 ]8 g" N
public final int getAndIncrement() {! q9 _7 @6 \6 A) f
for (;;) { 4 P, Z+ P. x# Z. {' D int current = get();8 t/ @6 i- Q' d9 _
int next = current + 1; 8 W* O G3 E2 ^ z if (compareAndSet(current, next)) . {8 @7 Q) @8 ^0 v( r! W' o return current;- K1 W0 ^) o) x* m! g2 l' I9 y; C# j6 J
} . A2 x- P7 q! F4 L; k) y( l* h3 r }% b- |7 ]' F/ H L, M( o. u
1 c) m, Y% F! Q) s9 x: rpublic final int getAndDecrement() { . q2 t6 u5 o) [: H5 U& Q0 B for (;;) {& { q; r7 z1 X3 p: ?& M
int current = get();* `( ^4 B$ [6 R& R
int next = current - 1; ( ` f% f' u( s6 | if (compareAndSet(current, next)) 8 _, z& k- v6 Y- k2 h# ^ return current;# x+ Y- ]6 ?" |/ r, k5 F C
}6 p) q k2 X8 W5 J0 f
} . @; J5 V& h) @5 @0 }8 K " s6 s' o4 x% }" Z' v0 w( @9 i# n! \
public final int getAndAdd(int delta) { 4 `6 H0 e q( o$ P! ` for (;;) { ( |. M+ a9 e( l9 t int current = get(); ) [+ w: b! N+ d5 U! E8 G. o. k int next = current + delta; : S; o- w$ G) y3 Z1 c: ` if (compareAndSet(current, next))4 I6 _8 u+ R( H
return current; 1 o) O- A$ E1 n% | }* i8 r2 u& w" u: W
} : {; m/ X) G! Q* d g* z$ l: G3 H6 U! ]
2 f/ [; V/ {$ M/ h/ ]
public final int incrementAndGet() { % t! m3 W7 [1 a$ s; ^ for (;;) {, H2 `$ W4 z$ Q: I% h# E
int current = get();0 }2 I9 H5 l3 J3 m" |8 x! ?8 z$ p
int next = current + 1; Z2 y! C G- V% H& R
if (compareAndSet(current, next)) ! P* f0 M- ^- p! l6 h8 P8 W return next;' V- q' _ @7 R8 `% u! h8 ^& W
} + q/ W# [6 l9 p) ]5 u* ~ }0 N, v2 w) r5 I3 C
C& T, i0 h2 Q% y2 Y) V! j8 P/ n % _2 D' k4 i3 t( Y. Upublic final int decrementAndGet() {+ N6 a4 ?9 M, n {: X5 _
for (;;) { # ]) J( g& @6 t int current = get();( O7 }. Q( e, J
int next = current - 1;; T- O3 S- a4 T" y+ U; f
if (compareAndSet(current, next))3 I4 g, o a& ]; u: _
return next; 9 m, M5 f. C* l3 j& k; l7 Z9 B } 3 ~" ?, \' P t _% {( L }! {' z! \( H2 E* B7 L
8 @3 |* q# |" p- ~
. Z9 ~$ R7 f8 p2 z" ^/ r Q1 \9 Y
public final int addAndGet(int delta) {" E$ v; H1 A6 |, o
for (;;) {# o9 v+ m; J9 D
int current = get(); * T) z0 f" f- P6 w& } int next = current + delta;: [0 Y3 J* y1 R! ~8 C' S# w
if (compareAndSet(current, next)) : h; y/ e l% f# J( r* y return next; 3 H! N% P- {( ~$ C3 ]7 t }4 u# @. K% X. z( A% E; S# [
} 2 C/ J4 X0 c% k ! K6 o d$ p2 ^ u7 Z3 c, y$ P. K# Y; |
public String toString() { 9 y+ _/ a: N z; j6 R return Integer.toString(get()); @8 ]! G- W$ L$ k2 n# I
}" ]/ q C: B3 P( \, ]
) a/ a1 U4 H3 t; M9 b/ ?; k# o0 R1 z' B$ Z6 G5 _
public int intValue() { + d3 B: l/ m9 h return get();: s8 ~0 u0 y. I8 D4 j
} 7 S2 p3 P$ K" ]. T5 @ 8 X+ N4 ^# H8 x4 v! e1 A: O; b4 _$ z# m
public long longValue() { % x! _- R- q4 O# f! D$ | return (long)get();- b1 }# k$ a3 | x" F: v8 Z& t8 y
}0 d: j( R* v% a9 `; V
1 Z5 F+ l9 _7 V7 }. @( [# P
1 g5 d7 a6 \0 P, e
public float floatValue() {2 u# Q5 T) L/ V& e# z& h. g( m& L2 T
return (float)get(); 0 C2 ?. k( y4 `4 O. y& |1 v5 R } 2 x( V# R# @: c3 h( M + z. t& l u3 ]" t+ k , m2 q& G0 \, ^8 t& u4 Bpublic double doubleValue() { 9 W) i& z4 d' k; N4 a& ` L$ M return (double)get();( K5 ^) A& S# n$ f# S4 c6 W
} ! t: {, X% d# v- ^--------------------- , A+ Z& ^8 Y, n
作者:feyshine 4 j9 }* E J" g. c q e5 l- P来源:CSDN 3 {. Q: o& w& }% J5 d2 r9 k5 I3 x 9 d5 \! `) ~ C( V$ x* R U& r; w$ L& j7 x" x6 R