AtomicInteger类 ; L9 r& p( R( E1 e7 y & E- \( ], y. t' k; {这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 - C# y6 L! s& M+ z3 d) o: V6 X. o2 c; t0 |3 f
重点内容 : D' K5 ]: u+ x0 _ & l2 ]7 L. @: h6 t$ S: M- s属性: % _- L* ~7 j" k, `9 Y, Nprivate static final Unsafe unsafe = Unsafe.getUnsafe(); ! C8 X$ h- k: p0 r1 J 3 I$ s3 A' }" @: C+ O% D2 M. j5 fprivate static final long valueOffset;2 ~3 e0 c2 F$ J, P# y2 _
: q8 Q3 U8 a+ k8 F1 Y
private volatile int value; ' S3 g3 \8 o- v- e/ |# P : Z0 I( k8 [4 f$ U静态代码块: 1 H c/ ~5 U. U# K; Y3 |& b; qstatic {: k* W/ ~ T0 r! j/ }& b; d
try { 7 j: u6 B4 y0 P6 D1 ?9 N' H/ ^, l valueOffset = unsafe.objectFieldOffset ( \/ ~5 o& {! m7 U, q (AtomicInteger.class.getDeclaredField("value"));* m3 \; b6 I* C# {6 I
} catch (Exception ex) { throw new Error(ex); } 6 x7 j/ y* k; U' U5 N# r }6 A; o5 M' ~ ?. e$ E7 X
--------------------- $ Y% r) S0 m6 @) |/ g5 ]& l; d, s5 Z# z构造方法 1 ]0 I& V! \, x2 Dpublic AtomicInteger() { 8 n4 {& h% j! Y- J( `7 w# J } : w- s2 S" d- D% n9 v- f# _. S: e2 I8 P$ o9 {1 `( j
public AtomicInteger(int initialValue) { : M6 ^9 w( j. K% m5 P value = initialValue;5 t0 O/ S/ G# N* u. \3 [* n
} 9 p" Q" x0 |1 a" I& w3 H, i, w$ m- U' b4 o- H8 d) @
对象方法:5 j' h. h& K `: P8 Q
public final int get() {* ?4 l. Z9 M& H" p$ A8 {# f# I+ g
return value; + U% H- _( Y3 k: K8 a/ o2 E; H } ( F5 z9 V* w! a! ]' V3 ?( Q/ V1 K6 _* E5 F
7 N' R% i* R* ]1 m5 M+ K7 Q
public final void set(int newValue) { , s4 j% q' @8 ^; C value = newValue;' d% \* m9 i4 F# {
}, X& ^) H$ m9 U1 \/ k
# i8 a$ @, Z* a% N& M! B% r4 L: G3 N+ e: j
public final void lazySet(int newValue) { , `4 |) P9 S* d2 J% V$ P' o2 d unsafe.putOrderedInt(this, valueOffset, newValue);9 @6 b& M& T; n8 M r% O
}1 g9 t( T; q+ q( d/ ^2 q: d$ c$ l5 A' K
! ^( f* j- T8 e* q' D0 i' g* s
7 a" ^6 H$ U/ \9 ?$ b
public final int getAndSet(int newValue) { 7 E: ^, ]& B" \! h3 f for (;;) {( p) J( s6 O" X+ x# q0 @7 ]( n
int current = get();3 ~1 U8 N: h$ \
if (compareAndSet(current, newValue))* f0 ~, K3 ]" b$ J7 q$ `* [
return current;2 k* F3 g+ v9 [/ a" s
} P- t: n8 O( g( |1 Q* t } ; a" A/ J# W' K2 a% C 9 M3 L3 ^! t# B, p2 _2 ^( }( o8 S, O0 Z' n5 i% f; H( r& q
public final boolean compareAndSet(int expect, int update) {$ d9 [4 r- M$ U5 f
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);( j& b/ S k3 y7 c
} # F) s: _0 N5 S6 B* n" A0 H" q & s2 W- s4 v# L2 R7 x( q# v O0 I, n: d9 {. |$ {8 [2 H
public final boolean weakCompareAndSet(int expect, int update) {" D3 d7 q! i" z& d: K7 W
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); * b7 U+ D F: G+ _5 S }6 b3 Z' M6 I% ?& x; \' Y
) L- E U/ v7 Z* L
public final int getAndIncrement() {& B; {% M$ p5 H) \2 {, r! |
for (;;) {* P6 L% j' h- x5 @2 i) k6 A5 F# W \* D
int current = get();; [. ^, V0 L7 h7 `- Y. D( b
int next = current + 1; ) O4 W _: b( S0 @ if (compareAndSet(current, next)) : H, A6 c8 \* i. \# O return current; g+ B' h, P& M6 x+ z* R2 X
}/ |% G4 a. ]5 J+ M' c: ]9 X3 I( z
} % d, J v1 N0 t A9 z; m, D) ~: C. Z
public final int getAndDecrement() { 4 c4 u. V( p' g7 v for (;;) {5 r# W5 E6 u6 f. v: Z" H# x
int current = get();4 W. j: v! ` J3 [4 d% B2 W* E& f
int next = current - 1; 4 w' m; e/ |# i7 T- f+ Y if (compareAndSet(current, next)); @8 m- u* l( t S9 h
return current;& L0 b v8 j& S) j& r
} 6 B( n, o+ H: {2 I( }( d } 8 J* j9 z, P; Z3 G9 ^ + K8 P- z: U; e. K4 R* p: L c, Q" i# H) I/ W+ W1 N
public final int getAndAdd(int delta) {( X! p: y- B/ D* T5 L
for (;;) {, E9 | ^0 e, k4 u: U s" _
int current = get(); # j# M& M. J" A' G int next = current + delta;3 I$ S3 w) Q& h. T
if (compareAndSet(current, next)) ^8 w6 \! K; j/ W& S' b& c. n return current; 4 \. @& _7 T4 c1 r } 1 T: B! M, r7 O* ^0 Q1 ^ }! h! j. ?+ g7 d
! E2 h. W/ c+ a 5 ]2 [# E) G5 `public final int incrementAndGet() {9 v7 L, ^' x. I0 K
for (;;) { 2 s7 }& [% d" Z) H+ K int current = get();- ? W9 L* j [( z/ U; E
int next = current + 1; ' \9 m6 z2 P; B2 E if (compareAndSet(current, next))! g& T5 p1 B6 F& m; g8 Q, n
return next; 6 I. j, `% A/ \ } , u4 A# i8 L! A$ _2 I }5 g6 _9 q& }9 K4 C
3 M$ u, o4 c3 h2 j* o! Y+ m
- _0 k; c- h7 {8 C& g) [3 A0 I r
public final int decrementAndGet() { 4 E% z1 j* K: K8 Q, l3 \( _2 H9 j' X5 i for (;;) {; @9 S3 y) N; K. p
int current = get(); 0 [! a# K7 O) n+ p* a int next = current - 1; & x8 I/ @# L( u4 Z/ q2 E( k: k; } if (compareAndSet(current, next)) 0 A" F( Z# l, B0 h return next; 7 }8 y( G/ R% P7 [6 B }9 q$ F8 Y) {( s5 |2 E/ d( V
} ) G) p4 I' W1 w" O) j" d- x: J & S1 F& o9 M4 q5 y8 W/ t5 r9 _ # z: y7 s/ b) u" p* ?public final int addAndGet(int delta) { , ~$ K6 y" {5 B, l for (;;) {* r0 I" o" |& m+ s
int current = get();/ a$ F; R% j3 |% q8 {* b
int next = current + delta;6 i5 @' m* X, z' N6 C
if (compareAndSet(current, next)) 8 P$ E: B0 H$ x% Q# w return next; / U5 X4 i) q: O }7 N5 b! o9 U+ H# d7 h( d# Z
} 3 |, P7 x( z; s5 d% u+ D& G) J, j# | 7 c9 V9 F* s' F* G5 Z; F6 ^ - f% e' h6 r, |1 S& Ipublic String toString() { 2 ]! }1 X/ ~6 O( m5 G7 Z- O' b return Integer.toString(get()); . }, ?3 H, S4 p- g" X' X }( U( O. N8 t; G, \1 U
: x [7 O0 {0 J5 M7 N; N! S, ^0 w" @
* ^& Q8 y2 N/ N' p# q" n( Tpublic int intValue() {. O* x8 V% x: l/ y+ @5 T6 u6 H
return get(); + }) Y6 k8 C5 O# P% R }. G+ |5 t( y1 @' K5 s: o
2 ^' D8 O2 @8 D' t' N ; _/ c" y7 n; x1 W5 Kpublic long longValue() {; [. k) q2 i6 D- E' w; n6 S) B
return (long)get(); 0 b7 f* o# R& O& h/ w } 2 h1 |1 ^- l) I+ R4 g M4 t" J: p4 T' k
! r @% u7 X: F, J
public float floatValue() { 8 J* M0 B ?# ?% C7 B return (float)get();4 r0 Q. l# o/ F( P
}1 D8 A5 ]0 }# m4 C/ m
: t% {5 p2 }1 W& i& v' p O' ?; o+ [4 T" ~1 G5 [7 l z" d; \public double doubleValue() {9 G& a m. f4 L
return (double)get();8 z `+ V9 [7 o
}: Y# Y2 o! g% h" g* G: V
--------------------- ; Z# Q- O8 r( X* G
作者:feyshine . K z( M% q2 j' T. L6 V3 Q
来源:CSDN 2 y$ N0 ?4 L" h9 d5 i9 N: Q% S 0 z4 i) D* x! V, Y ! O: R" f: x5 m$ ~ " [. e7 ?! D4 E5 D5 \) x$ Z) Y( i8 f- m5 ]: I% J; S) k
' O/ V, l t5 z- l4 ]