AtomicInteger类# H5 { l5 h6 G- h5 k
5 R# r9 z" M0 o5 a( }* C7 W
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。7 t @" m$ R" ^ P& E+ S' R D
( u- l+ t ]) S* s* B* n; _
重点内容 2 c7 z, E% f1 T; T+ {- R2 J / f/ V' A+ m. i ^属性: ( {1 c: S# l& \9 A0 Bprivate static final Unsafe unsafe = Unsafe.getUnsafe(); 3 [' x+ [4 {2 [$ P3 f$ a; Z$ m" x, g" y2 y' z
private static final long valueOffset; 7 B P/ t7 o, e9 o9 h$ Y; K, X$ X0 g( T* J& k. f
private volatile int value;5 H: r5 V( I$ H- z7 N
1 f( f* ]$ k" `: ^' ?+ Q4 q, q/ j
静态代码块: , a) t A, M T, y3 ystatic {0 G( K3 }! E% x5 I# U7 N
try {* `2 I: f8 M1 P/ p) j
valueOffset = unsafe.objectFieldOffset 6 K# m! k" w! @9 ]! j- l4 M (AtomicInteger.class.getDeclaredField("value"));" y$ ^1 E+ [1 S0 q, t! o
} catch (Exception ex) { throw new Error(ex); } ( Q( @5 L1 [. T$ h l } " O4 n' R9 k$ N) a& Q, R--------------------- ' ?# T: w$ i) j! P' n5 @8 q构造方法 / G, g. H* `1 k9 A. j$ Opublic AtomicInteger() {/ |6 h' q7 R) _
} . q9 w7 F. y* x0 I) d. F: M, I f" ]5 k& T
public AtomicInteger(int initialValue) {( w; q+ S4 O6 A" n6 n6 l# t
value = initialValue; 8 i' h) f7 t5 V9 E, s2 J) t }2 O3 }# s1 r4 [
* U9 q# X$ ?: {
对象方法: ( O$ M" V2 Y1 u5 q. q public final int get() { ; |3 c8 h2 w; W/ e return value; 4 m) Q4 Z- \& U, ?& U+ q# | }* _5 w$ Z' ] N2 U8 x; F; ~4 I
1 K* _& Z3 q% ]/ U) X ]
7 ?+ R) L: z2 t8 ~5 y# ]public final void set(int newValue) { + K, z9 U1 F. ?2 `. v7 r value = newValue;; `* F/ l1 Y: N- k& _
}8 b" l3 J+ f& M7 Z) X) `, N
6 D- ?7 `/ A, Z# d3 k3 v2 }9 y/ d0 {4 H; S+ Q5 q# _
public final void lazySet(int newValue) {. |& Y3 ?+ b, b
unsafe.putOrderedInt(this, valueOffset, newValue); . f1 }4 `& f& Q8 z } 7 y1 J% H' A( |, W3 r! _ 1 C+ K. B! B3 @# w( q7 x9 ^8 ^& F$ v' C * w0 Y- u# d7 _: [9 R# Y( o7 cpublic final int getAndSet(int newValue) {9 w& V1 R/ d4 }, H# d% P" m
for (;;) { ' m/ V9 W$ k' W( k* u/ W" \ int current = get();3 \8 z& O7 w9 e) T( l! R
if (compareAndSet(current, newValue))4 P4 a( M5 [" U: a
return current;1 z+ v2 ~" i. i% `- d1 ]9 x
}. X, w" W* A* C- r+ P
} 8 c. M; k$ ^4 }1 f4 q8 a4 U- Q E& ` r6 H7 q7 J
; Z+ Q4 I+ a- Z% w( g
public final boolean compareAndSet(int expect, int update) { }: S2 w' w' H5 h q; H' j. M5 f return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ' G4 `& m7 B6 V, O* }8 W9 J }0 v, y5 ~& R; H, e5 `
1 t- L, n) G6 C
1 v) m: c5 e7 f. b
public final boolean weakCompareAndSet(int expect, int update) {' ~! a3 y# ~- V# r" {. w9 [/ L, |
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);, Q% F8 o: u' ]4 s) Z3 f: V
}1 `# o/ U8 ~* U3 H( O* o
6 p, C+ c& G. _( B: P
public final int getAndIncrement() {# p& m2 ?0 A6 P
for (;;) {1 y3 b ?8 M K y& m3 Y
int current = get();% G/ @0 ]' _& U0 Y( g
int next = current + 1;' J3 ~9 S9 `! R% o% C% M
if (compareAndSet(current, next)) ! v4 a% b( @" `5 }# [ return current; 7 S) [: @4 S% n- ?( q3 P7 y } ; c& T- h: V$ `- k0 ~# s/ g l }/ W8 H8 F' ?% e. x, H1 j6 i. A& g
6 P2 M8 T9 N' c- K% f
public final int getAndDecrement() { # S# F! o9 |* r" u! D& b for (;;) {, h8 s( w' K. G& F8 ]$ T, a
int current = get(); ) q/ x8 i) [4 X% C' J# ?' @5 O) F int next = current - 1;$ p% i, h* W0 m3 J3 h
if (compareAndSet(current, next)) 0 R* e/ m/ Z: P return current;* e5 W E3 n( @4 W7 w) z
} 0 [7 z# E$ N$ \) O2 |# [ `3 r$ H }5 d& U5 V$ M. F5 z5 a8 g9 @
! {3 P# ]+ t# @0 A3 B6 c& G# }+ E7 V/ _, W1 h& T: \( Y! Y
public final int getAndAdd(int delta) { & f6 Q! m7 b0 y$ F% t8 ] for (;;) { 2 {* ~* \- y Y- m+ M int current = get();5 ]8 n( j' ^2 {& h9 a! J8 Z) y( W
int next = current + delta;6 K( m- ?: W8 c9 ~5 Q
if (compareAndSet(current, next))! z* O. K8 L/ u9 @
return current; . S" X6 N( F* @2 s* }9 z }9 [0 h l" u. ?$ X$ A' C6 @- r
}$ O7 \, R5 U* [3 T+ |% h
" _3 G8 l+ q8 A' O" i! J. @
9 K7 _& C: T- J
public final int incrementAndGet() { ! I; p% d' ]. R, t3 N# \ for (;;) {+ D. L7 p8 m% j
int current = get();8 Z3 Z2 X: ^$ D4 f$ T
int next = current + 1;. c7 t5 m) J% F+ j, L. ^
if (compareAndSet(current, next))' R; X8 Q% `& S6 h
return next; / E e( n* U: F } 9 ?+ ]' u! W( b5 E& p) j& E }) u9 N- I$ |. M. b2 F& K7 c' q' h
& G7 G( }0 f* ~5 ]/ o : q8 F6 P! N5 Y0 ]+ R! epublic final int decrementAndGet() { $ F9 L5 d' _1 f for (;;) { 3 p: U1 ]; I0 {7 w8 v$ a' R- y int current = get(); 5 ]8 y" o# p b# b# L U' I" X int next = current - 1;( y. X& S; B: E1 O
if (compareAndSet(current, next)) 6 }9 [3 X: l4 J return next; / J( z f* O/ R5 C# W }( j+ N' C# c/ a' U- c9 |
}: D6 I" J% L% n: S) B; l- ]
0 `( p: A, ~1 g2 D% F# }9 O9 x& f7 G5 f0 m- U
public final int addAndGet(int delta) { " z6 W. g0 N$ b0 x for (;;) {4 n, u$ z! {6 |/ K& m! f
int current = get();9 X( [0 m+ u- L3 s0 L) M! {) }+ |
int next = current + delta; " R1 a; T% Y* j. b% q& {$ f$ s* F, w if (compareAndSet(current, next)) 9 `4 Q2 x3 c$ B: N8 C- U return next; 1 ?7 m$ d$ `! c0 q9 |% P( W8 H, S } 4 u! B: |/ f w: L. b, ? }# l5 J8 T0 F1 L
& F# O; G" ~: j' m1 F+ t8 w. q/ i& D/ P7 }: _0 u; W
public String toString() {5 s. X8 Q7 ^7 ]1 s5 y. H5 t! ]5 _
return Integer.toString(get()); / g T" B+ U; _2 i8 t6 ^4 z3 Z }. l' ], A8 Y1 Z5 m0 m4 b: D! ^1 s
9 o5 X6 |2 g1 k9 j+ f6 i' X4 O- o4 |# Q0 o) a) O8 D1 e% e8 f
public int intValue() { 8 j8 k& {& z1 G9 [8 U return get();$ l3 n6 m: X: x+ E: k2 G
}% v" P1 a' o3 t9 n$ z/ }
3 t0 S. V+ C( f" N, j4 c
6 ^- f' u8 z; h/ ?public long longValue() { # C" c$ U" C6 i return (long)get();2 A) \" {6 l& x* ]
}/ I7 p# j# y6 ~; E. E
% F4 S' A- |6 @+ X! I 6 C V+ p5 I1 C. |" |& d. lpublic float floatValue() { 0 f" e, }4 {) @# E9 f( r$ U: R$ r return (float)get(); ) d6 z5 m- k/ _ }# E; ?, ^4 _* s* P, b
* M4 g5 m0 O6 {) k' B7 _' X
$ B: R. x- e' `6 v+ j
public double doubleValue() { ( J5 R, f* G6 ?, O9 r& i; k return (double)get();/ U; u0 [2 e, l
} % d) H9 m" ^ D8 ^--------------------- ; c$ A" t. M$ ^5 B# h* E
作者:feyshine 3 O( \+ v6 M4 I
来源:CSDN 6 j) r: t: L; `& R * h' v/ u* z& p5 V; z% y 6 ~% Q3 W; M \. n* r' p! G& N8 S1 B1 n% M, q. `
7 q5 h6 D/ }# l4 y
6 ?- ^3 X' e# E