AtomicInteger类1 i( S! g- ~" d% l+ B7 [ u% w
4 P$ ]2 P) H- E! `, j6 D$ ]/ m6 t- s5 M
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。8 t) q$ ]6 D- A3 E: T, x
0 T" j8 U# S6 x9 @0 O! S
重点内容, r! g4 l f' K$ Q. N8 C8 u1 E& S* h
/ A! h0 D+ h2 L
属性: 0 j/ s+ o1 _8 G- A2 Wprivate static final Unsafe unsafe = Unsafe.getUnsafe(); 6 a4 t. s O; l/ X$ R1 d( U( t7 _" _8 s0 y& F
private static final long valueOffset;4 n7 `% F. q4 h# q/ M
: h. [) W' k4 [ U! hprivate volatile int value; . h+ x' H" p8 W( o. k, ^/ r! s / q# h, V8 ^* U; }4 _' C4 T2 I静态代码块: 8 u! e* R( E. V! \( Cstatic {+ H3 u- W7 V0 E2 L2 ?* r# o
try {5 v/ w2 J Y. H+ B5 x8 s7 r
valueOffset = unsafe.objectFieldOffset# M% d: a8 _( W* w. @1 L) z: E0 `
(AtomicInteger.class.getDeclaredField("value")); # K% k" @- q/ Y" k; R } catch (Exception ex) { throw new Error(ex); } ) N% L& R t" V( W8 r! A }* u7 f9 M& _" w2 W6 M M
--------------------- 7 Z* K2 H" M4 y' t6 G
构造方法 8 K! p8 ?" z, i3 [7 P( E+ Xpublic AtomicInteger() {" [. X: ~# a! a( P3 ]6 v. ?7 u
} 1 P" Y8 a5 ], F4 p& T$ b; J + E. ]% X: A, o; T. [/ Vpublic AtomicInteger(int initialValue) { ; p) y' h5 p. n, x, @ value = initialValue; 7 P O# @6 O# \* x A' N }+ {( P2 U* C( C3 \6 c+ k
1 y1 ~4 x. T, s6 n0 w
对象方法:1 |9 D# k; }& t7 w
public final int get() { 1 \1 y: r0 V( G. h$ S) m8 S return value;5 K% n/ \$ N) b2 e8 Y9 R6 A
} O# r' n4 B- \ % C4 S" {# R7 Y8 h; E2 ^0 U6 a3 [. A% V* f( w7 t
public final void set(int newValue) { / U1 o/ R% L1 B" K- | value = newValue; 0 e2 C3 I: q( |6 D) C- C } B: N2 I0 h" _
% r# w% |! ^9 q. v5 s% o * U5 s$ U; ~0 k# m8 Hpublic final void lazySet(int newValue) { . i8 X6 P N: P unsafe.putOrderedInt(this, valueOffset, newValue);; V4 M" V s8 I2 e8 A _7 O
}1 b& W6 V& f1 K
" p# T% P d6 I" E% y; F 7 D1 \4 [# w# F% y$ U/ n' @public final int getAndSet(int newValue) {$ v2 d0 u6 I: c$ M. K$ Q& Y w
for (;;) { ) o/ _3 x2 F$ A2 m int current = get(); ! V# J, J' g% z b if (compareAndSet(current, newValue))* Y" `- h3 g) [% {% X+ T
return current; 3 D/ ?' `; t7 O1 G6 G0 F( L } & F# x& w0 G& U8 Y } # i8 \5 d" |2 I; e5 Y, U* I! [% C& C, q" I& F( u: A
$ o# n8 X' K% i* dpublic final boolean compareAndSet(int expect, int update) {6 w( G: J" I" t# t7 `- D
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);" M! i; U5 w s
} . {, D3 U& V d9 y( V3 E0 G. H - A- { X. m. s$ ? : Z) ]0 |3 {/ ~public final boolean weakCompareAndSet(int expect, int update) {# }+ V) f. \$ y7 f8 }- T
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 3 F, W2 O" U+ a B* h } - u9 M$ p X9 X6 F ' m1 C% r' T, E3 B# X* Bpublic final int getAndIncrement() {; D6 ?( ?% [' e. E. C" F
for (;;) {9 U' ]2 Z) K0 M
int current = get(); - L4 j: s! }( o% b1 N int next = current + 1;0 [4 ?2 \7 F* ?0 a7 g* Y/ X
if (compareAndSet(current, next))5 x" ~0 b2 x: o d5 z
return current;$ m1 C; d. v( t* V
}8 I4 C+ N3 y; e3 X0 Q
}! ]! ]1 @1 V7 L c# E7 J4 ^8 S; i# D
, N8 S u1 l' Z* Q% |public final int getAndDecrement() { U0 f* M4 S1 n% X1 q0 e# ` for (;;) {5 d5 Q4 I5 R) n1 d+ J2 R- p* b
int current = get(); # r6 j2 z. A0 L& ~" d+ Y( \ int next = current - 1;( H2 w& P* k I& ]+ a0 q7 \# \
if (compareAndSet(current, next))6 _, i6 P" }0 o* E
return current; 2 Q( i/ a ], m# o# a+ L% O4 c( c }% V# P1 C* p8 ]
} 9 `) x4 {3 J# R4 x 3 t5 w5 U3 i/ {6 u+ @ 3 D% `9 ~, F7 X. j. U" Bpublic final int getAndAdd(int delta) { 1 V& ^0 ?3 Q2 e7 z& G2 z1 E for (;;) {6 G& s: F- T9 W2 ~% E/ O0 E: ?
int current = get(); 5 @1 P' z* \+ k% I$ ?- } int next = current + delta;, |' y; {: K. c. b
if (compareAndSet(current, next))7 X; f) C5 h Y# e* @# g- {
return current;6 d' b/ c" Y9 w2 ^- `/ D+ U" o( o
}- V, h% U0 Q; ?; O$ J4 u
} 8 A1 ]8 y4 J, g# {/ Y- l: o0 z, v; X# a7 b* Y6 C& T) @
' |. C% o9 c/ J+ O1 _/ r3 s3 E1 Xpublic final int incrementAndGet() { 1 S: X K0 w" g9 R for (;;) { : H1 i1 y) w& z3 H: G! V& I/ | int current = get(); : x; _" h" `+ \& ~) V int next = current + 1;+ v6 p) Z' K3 M7 Q# p3 S
if (compareAndSet(current, next))# X, s* w/ F( i+ m" _$ Z
return next;* t% i' g6 m9 f' N9 N9 g( K
} 6 G) o u# q+ T# x; X } : f( G4 Y- K7 Z4 Q6 _( a# q: h& f 8 f! f) J* ?. A6 n B+ R / [ `1 H1 O0 U1 U- f5 }public final int decrementAndGet() {1 m X+ O! h4 q6 Z2 h8 L/ z" k2 j
for (;;) {8 h! z* u. i0 F; P2 h
int current = get(); ( y: e" G# U4 S5 s" k7 o; v) t int next = current - 1; . c& h- C5 v: h0 [2 F A# _ if (compareAndSet(current, next)) 8 w m3 u3 i5 |# X0 K- ~/ q, p' w return next;" V9 B- W# J( q0 r8 A
}1 |( u' b3 {8 C: ]
} Y. M) h6 J$ ^) U# u E. L$ e
% m+ N! k) w" W1 w8 J/ ]0 A K& X, H; M& J) x' d
public final int addAndGet(int delta) { N# O3 s! U& q; y2 f for (;;) {2 m3 L0 t4 Q7 b- J$ e
int current = get();% s! i: a2 I0 {* y9 P2 t
int next = current + delta;( F8 f: b3 n+ J- v+ l/ R
if (compareAndSet(current, next))( f# U% `; I" p4 {6 V
return next;* l* `9 \# \/ r1 v7 w
}; U$ t+ W" g& a. n, q" T6 e U2 }
} 2 t, ` S; [* @( A. l 4 y. F) e( C1 P! l 3 z1 g% u3 e6 S: U# m: \: Ypublic String toString() {8 w( K+ r: I4 b h# g/ M
return Integer.toString(get());% M' H3 B/ a, z8 z+ s4 f
} " S$ P5 {2 h. q/ [, r% M4 }3 l( ]/ L" o8 q* j3 h
' i S+ g% G9 \5 b) _
public int intValue() {: O. ]1 a3 k- T7 q5 E
return get(); . Y8 d w9 D; c! r6 t } " J9 x" T8 A0 V9 [0 i7 E! M6 d4 f! v: f
0 c+ T' B6 q |' @" Z1 J- ^8 Spublic long longValue() {& _6 S: Q/ e0 T3 q" h
return (long)get();# p; A; [- b% w \
}2 A1 Q( O9 S2 n. v0 {7 K
( B( Q8 [+ ? e; y1 H k8 h
h+ _3 D" D; ?% x# {- h; e1 O2 \
public float floatValue() {8 m' n; I# V$ W
return (float)get();8 T {$ R9 N& g; F: ]7 a
} % N& t. U. p' a+ f; z, P4 G: V" R ! k, s4 ?; b9 C( z' F) D5 H2 H7 K3 \2 K- k7 ^
public double doubleValue() {3 I8 K6 B6 A1 Q
return (double)get();0 }, G- X5 A: X# F7 v- A
} D/ M* \ h+ R
--------------------- 4 W' T4 X4 d. S) ^$ B5 l
作者:feyshine 1 N4 [9 I4 }! u( u4 O% |7 ]来源:CSDN $ a$ f6 `) T9 N5 B. A) x, U/ A
9 J0 ^3 p* Y1 R2 q; i- G
; r' b: m! ^$ C6 N! i& n/ L$ s! T
- L& `7 B/ E$ t: L( E( G0 o