AtomicInteger类1 j5 k0 y5 n: I% Y, I" U. ^9 M" N
. r; d1 V" q# F
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 * v8 `4 i- \8 ~- H2 ?' N3 H, G1 s) Z( i9 ]0 O
重点内容% ]' [7 j3 o' e" i5 F% ]
& p1 r; K, u& }: { ^2 `+ V
属性:% a1 z& F w3 P& i
private static final Unsafe unsafe = Unsafe.getUnsafe();* v! D6 l+ l. g" U, _; f
( u6 [/ B8 t! G/ I. F: O8 i5 h
private static final long valueOffset;, h( S( O* H: X& V+ t
6 V' z2 ~. B- ]! I
private volatile int value;" {/ j! p* l; e" `% C' Z
3 D' }. S3 V! ^7 e5 l/ I静态代码块:) ~$ L5 H* A: Q D; q! G
static { % W5 \! R8 P$ E! n try { 7 d3 w v! M4 K+ w+ R valueOffset = unsafe.objectFieldOffset3 W1 T6 X0 q3 M2 [, E- \ b
(AtomicInteger.class.getDeclaredField("value")); 2 H) H! c1 y! y4 p% M# Z) \ } catch (Exception ex) { throw new Error(ex); } 1 c! }9 a# }( Y* e) _3 ?/ b: c9 x( e } 0 e% v) E* B8 A+ W; i--------------------- 7 T" l. }+ G( ?% y% J: p( G
构造方法0 O/ i0 V2 d/ B8 D1 @0 ?, k
public AtomicInteger() { 9 ?1 w$ I3 T* @8 [3 d" l! o }& k$ T6 Z7 R/ b: u' N' _& e
) I Q* g0 }; `public AtomicInteger(int initialValue) { 2 y0 m* |) T6 f5 V: F value = initialValue;$ a' b4 T1 B. ^* C" a
} 3 J# N3 f- W! \$ G6 d4 @3 A1 j% t, v/ ?3 O$ F& W
对象方法:* o. r5 M( y2 k; o' ?5 C. R
public final int get() { - l' r" a% ^( L, E return value;7 r, f2 H: _/ v
}1 i% u8 s( _: \8 e
9 v- j% F" e6 ]* h6 p. p# h. q9 V _; t
9 X h8 O6 q: V. M0 U3 [& g3 h( n3 n
public final void set(int newValue) {5 d5 T* ?8 [1 R1 s
value = newValue; . v3 S+ I4 L. B5 |5 ]3 l: ` } 6 ]8 Q2 I( f6 V1 T0 s. M6 ?, e8 D7 d5 }. S
5 u, @; U. s- G6 vpublic final void lazySet(int newValue) {) F0 }9 d/ @/ I: j, f! A4 b: p
unsafe.putOrderedInt(this, valueOffset, newValue); 7 S! x. o. s2 l) k* s- A' A5 ` }0 L9 X$ J1 w5 X% ]6 g1 \" p; C# _7 r
$ j* `, v9 X- r$ u, @9 g9 ?
: q6 {* F! I: ^- u6 O
public final int getAndSet(int newValue) {6 ^0 Q* R' x* U) g, ?
for (;;) {, i4 x! O+ m9 V' U7 e# l
int current = get();4 n- @+ L# S8 k: Q- i
if (compareAndSet(current, newValue)) . n0 L) t, k3 r$ q5 l( R return current;# l3 c8 y' a- O) y& Y. k
} 9 Y v! K: R: P$ l, D0 i% z }4 T3 b2 N+ y ~$ L+ N
" T. Q# s, O- U( ?/ t8 a& w, o8 j P% |# P/ Y
public final boolean compareAndSet(int expect, int update) {$ d. {# S7 ^/ a* T
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);8 t6 W1 I7 o6 P+ U
} 9 T7 l6 Y9 @3 q' `% D9 V. e7 x4 |. u) |
/ J( G& n8 W6 d3 Q3 C% q! X; b9 D
public final boolean weakCompareAndSet(int expect, int update) {0 [2 M, Z9 G# ~ L) q
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 0 {) \( ~! r1 }* N } 0 C5 A5 Z4 J$ K# }% q3 G" E/ k8 e9 R" y4 Z
public final int getAndIncrement() {6 r3 t. L" B9 T" i3 _ D
for (;;) { 2 _1 `& r, |' b2 k int current = get(); / ^( a. W m- [. W int next = current + 1; 1 O* i$ ~( a; C: [1 o6 s3 J! m if (compareAndSet(current, next))8 e( M% O9 ^& D! m9 a; G$ J, V
return current; # a3 ~9 R& D) }1 c z4 N1 \ { }1 _. V& r% ~1 k7 A5 O, W
} # j7 I/ C2 h. f# _! q( n: z' v+ B5 ^% p
public final int getAndDecrement() { z" s/ ^# f& D+ m/ }, z6 i
for (;;) { A6 ]0 S: t- i int current = get(); 1 c/ a- A4 t# N% G1 }* S6 N, t int next = current - 1;7 D/ ~9 d+ _+ m, O4 D( O2 f7 \
if (compareAndSet(current, next)) 4 H* M% \& b. x! O7 @) l return current;1 L$ X. F# }# _0 {+ L% A9 A
}8 t; h* \9 H0 u' q; @
} , ^6 [+ j; x; h/ b, d+ t/ Q. @1 V C+ ]
0 v; A& g7 T x$ i) e
public final int getAndAdd(int delta) { / r: u& }! Y& u6 l. _ for (;;) { ( e) y% P/ S1 G. [# P int current = get(); ' [% X) p0 x) h5 S+ r' ]6 F int next = current + delta;! v+ ?- D! o8 N
if (compareAndSet(current, next))& t4 o: h- ?# Z2 N0 v; d$ V; A
return current; L5 U5 G i1 ]6 A5 Q }9 H$ D! l5 s' G/ q# Z$ a. _3 q1 @! @
}1 z' U# L7 R* b8 L2 ]0 R/ B0 G
1 e( W! [! ?, Z4 ?. x2 L1 ~- P7 J) U
public final int incrementAndGet() {0 |6 d! [" ]5 u( n( _; i: C; `
for (;;) { ' j8 l% B- O# h9 C+ U int current = get();, E) H8 o0 F2 O5 ]6 Y- Q& h
int next = current + 1; - M7 Z L9 O6 s+ S4 {' O if (compareAndSet(current, next)) ' d+ j5 s, x! J8 a3 A- e3 H return next;1 X8 p$ j( ]/ o* ~- x
}4 G ~ N! p4 }* p- o
}. h; L4 i% n3 {; v/ Q! {" V* a
6 N, l5 o" t) ^- Y
' C6 x/ Q; g" g8 i. p9 Ipublic final int decrementAndGet() {6 i7 R! c/ r# k/ R
for (;;) { / Z1 D" c6 l& o, c* C7 f6 n int current = get(); " H7 W+ y# G+ I" `0 [# ] int next = current - 1; - X% C( t% c% P P# y; _ if (compareAndSet(current, next)) y$ E/ s) R' d, `5 W" H) k, r8 Q- \, {! i
return next;. \6 P& W0 B* H+ S
}4 C& ]6 b" Q, P/ I
}3 W) L# n0 L+ i+ q0 Y, W' w4 J& I" L
# Z4 b4 ^5 n4 u) m8 I4 ^% z
& G5 o; m2 ^$ Y9 E! g/ M7 g" hpublic final int addAndGet(int delta) {' h+ i( X8 g6 ?
for (;;) { $ Z0 c! h9 |$ D int current = get(); , w( p1 `8 Y, }" |" F" @; g' g j int next = current + delta; ?7 G+ b( k; S) t' W$ h
if (compareAndSet(current, next))- d: }4 ]: o$ H; M$ p
return next;5 F4 c/ h S1 C
} + q8 s y+ |7 L2 B( ?( u }5 W/ m2 K8 P( A9 c
" }5 y2 k2 @) s. ^. I& y4 B3 `6 a) q+ a
6 O, X. q! r0 F. v' L
public String toString() { 3 _. N2 B# O% w; Y6 V* S return Integer.toString(get());5 ?5 t' Y' x+ u; R4 Z
} $ ^/ T h( t0 n4 _8 [ 0 _& g0 S& l7 T! j, o$ |8 S9 I6 S) d, o
public int intValue() { & X* Q% f& b* M- V+ i return get();+ s- W; l) w( C% D) R) B1 g+ H
}: B- E. t; v6 H4 u0 H9 a1 i
% }6 `- [$ b" U" O. D, Z) \% Y( \+ D! H5 J' a
public long longValue() { N" {; x( a( t; U return (long)get();% P1 F+ e. K) F C2 A# [
} 0 A2 d0 @! t6 C6 ?; q0 N- j7 e" E Q4 n6 L( _8 K
U" w5 j. V$ A6 I0 Lpublic float floatValue() { 8 D# \7 o3 P- N8 g3 L return (float)get();* k( `# e. `- d: L0 K* l
}& O: `& Z" ?- p A4 V# F- M
: d; t% g7 f+ Z8 h4 s! ]0 Q2 B
public double doubleValue() { # U) y# _$ f+ C8 {/ w$ B% y return (double)get();0 D( D/ \& U+ X
}" r0 m3 W/ |/ W X, s7 K7 X
--------------------- $ R* {& U4 F6 K4 W; E
作者:feyshine - x- T5 u" p" R1 M9 \
来源:CSDN - p6 e. P6 n3 \. B5 I
2 g d2 h' _5 y; A - n2 R1 f( M- e$ t5 e% p" A . {9 L+ U' V6 C; k- G8 n( j! o7 Q7 P: I: A& R3 I5 H. b
2 z5 o8 {* L: M: B& z! E