AtomicInteger类! }' ]/ a7 m3 M3 i! Y
- ^' U1 ] k8 z$ y& c% f& X$ ^8 E
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。, i( Z2 Z. O' _! x+ l. i& P
8 ~# D8 ^- L. k重点内容4 R0 b0 Y' E( Z! P4 M! m
% z: O- W7 ]" I$ h( P2 d属性: c y6 s6 Q1 Y6 C4 [+ W' t$ Zprivate static final Unsafe unsafe = Unsafe.getUnsafe();* n K m! k1 B$ m4 ?1 e' r
- g- x7 O% X6 Z- z1 B; v/ @7 B
private static final long valueOffset;. k5 L; a; B$ Q
8 {! G% y. o8 r* S$ }private volatile int value; # [9 P' J# S& |7 G; f- b) y( }, Y) j3 B- M M* r
静态代码块: 9 [( w3 F. r" Kstatic { # h. l# q+ n9 m/ j try { ' q0 \6 C% ?* {* T- ~/ F valueOffset = unsafe.objectFieldOffset1 C. M; p; G( H. X2 h
(AtomicInteger.class.getDeclaredField("value"));' a8 S; _9 C4 D3 G: j5 q9 R
} catch (Exception ex) { throw new Error(ex); } # M2 \$ D& ^# K3 b4 T9 } }3 Z# q0 [0 T' K8 V. }
--------------------- : A3 t) B3 U$ r构造方法 8 @1 l' n$ `1 x" upublic AtomicInteger() { - S8 D) L5 Q) D, o1 F } - D* F6 X+ L: w. `& S) H" u 5 U) i; @7 |9 J k# C9 [public AtomicInteger(int initialValue) { ; }; r) z' U% K1 Z% W value = initialValue;- x+ ^# `0 b5 p! I6 B% B/ h- x
}% j" ~; z* |- M+ ]5 g7 p4 }) ^
{( e8 Y) H5 W% T& p
对象方法:. F8 Z1 a$ v$ C6 E1 u x; i( o
public final int get() {$ g3 u8 X- F2 k; |8 e, D
return value; 0 [5 T* V9 h. b2 P, \- D/ { } 2 u) F9 L* e/ @% z5 ]7 A + y. F9 z7 N4 N E, \+ T1 D( T; Opublic final void set(int newValue) { ! w1 } P `1 _: @; B value = newValue; * w z7 W6 F& D }/ s+ V9 s3 M( { v. R; v
& g/ D/ Y1 J! \* t" l' w" k 5 s7 q7 O d/ X2 e' K0 r8 b4 Y" cpublic final void lazySet(int newValue) {: _6 j" L/ N H! R: S0 h" ]" X
unsafe.putOrderedInt(this, valueOffset, newValue); 7 O/ ^0 L# }7 f# O9 y } % m0 x V- R4 V+ ~: {+ H0 R8 h7 ]- K1 R4 p) F F3 {' c4 {8 f$ v
+ Y" Y; k, w6 Qpublic final int getAndSet(int newValue) {1 p7 Z4 b. x* o5 \. P
for (;;) { 7 D% G: ]) t- P/ Y* Z7 e int current = get(); 4 p% J W/ P Z1 [ if (compareAndSet(current, newValue)) ; ~3 `7 V2 Y+ G3 `% c return current; 0 s& C4 r) h" o# _# ~7 S+ p: O } & F( e4 n! W' ^# r0 W2 T% M } 2 x R; q( e5 r; S0 c7 h5 f3 \6 m8 J) k( z
) A0 @- }4 |, i4 zpublic final boolean compareAndSet(int expect, int update) { 2 k y$ ~) ]0 Q, | return unsafe.compareAndSwapInt(this, valueOffset, expect, update);0 O0 u& e9 u/ k% E5 L! z& l( U
}$ L: C+ I7 F* w$ q- C
( _8 @0 F3 w- M2 ~+ s1 I/ o- W ( Q9 j6 g5 G$ R/ \- P% epublic final boolean weakCompareAndSet(int expect, int update) {3 W& _4 E8 F8 F9 k
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);. P6 S( n" {) n7 y
} k2 }4 [7 o0 c: q' _) y
7 i9 @- ]5 q+ M U2 x' x' n
public final int getAndIncrement() {5 G: n8 E7 e" J4 g
for (;;) { 2 e8 b- P- ^; `+ {8 n+ L: U int current = get(); 2 e5 B0 Q, y1 H% `0 Y int next = current + 1;; H/ I! `# M" _6 w
if (compareAndSet(current, next))3 b+ f. e4 Y! R' g( O
return current;0 y! O4 l/ b% a- h, ]
}* V4 w: ?' x6 T$ L2 z( v) W9 O
} 5 N# J& N2 W$ g ; V. f0 X: v, W. `public final int getAndDecrement() { 9 z, @4 P6 H' t6 U6 a! Q% d& V for (;;) { + p: d/ }3 ?+ J& P5 M, [0 W int current = get(); 5 Y3 X) d. [6 m# I0 g int next = current - 1; : v, F5 A% h- o7 T if (compareAndSet(current, next))( A1 _# p1 ^) C H
return current;; E5 |3 ^+ w4 [" T: s/ \; a
} * u1 s; J- N( p n' J5 u# F }8 Z* \4 C) \% @8 G/ L- b9 `8 N3 g
! e. N* e% {8 s! ~0 N
1 j' t; g: U/ R
public final int getAndAdd(int delta) { 3 a) c0 ]. f* ]3 T9 T2 U' i for (;;) {* x; D: B8 b% _3 b' Y" m+ |
int current = get(); & [7 u, a& P' j$ q3 f. V7 l) ^! [ int next = current + delta;# D, H H$ j: b& ~/ y
if (compareAndSet(current, next)) & g4 i: P, a6 _9 m return current;" n9 o) @) U5 y8 x' n
}7 w9 B& T; l2 k' e& [- `$ t5 Y
} ! d+ X) h& P* ]* \: g( ]2 Q4 U 5 P4 n4 C4 E% a, O. p; Y7 k$ M3 @# K: P7 @
public final int incrementAndGet() { / P7 n9 X5 n$ X5 w for (;;) { ( r' H4 o7 w/ C8 E7 ` int current = get();- T, ]1 R2 _8 ]3 t# N9 p
int next = current + 1;' c" j5 s8 r) t" g
if (compareAndSet(current, next))6 c0 X( P a& F4 a+ ~+ l" H+ a9 `7 Y
return next;& Q" n0 M1 g. C6 K% t( r/ @: ?
}# ^! U- K i7 \) |% j7 }6 G' z
} % c; y6 x$ U% x9 U" R7 Y $ G4 o; U( X J( D7 b3 y 9 L2 E( P/ R; x4 Qpublic final int decrementAndGet() {/ i- a% K( C0 i# S0 t. _$ C1 E% n
for (;;) { - U, B, X% `1 |7 v& D0 m L int current = get();, Y0 Y- r4 L. r1 E4 _5 T y6 m1 o
int next = current - 1;% m3 e8 h$ l, P; I- K' c
if (compareAndSet(current, next))# J7 r# e" k) A4 N. S) M* p
return next; ' L7 s2 m3 j4 E( { }' e7 n- c* h7 X: `6 p: W$ q
} / u) @; t+ n/ q o, ?8 c5 G- L/ c/ d
: F5 \& f$ j- \
public final int addAndGet(int delta) {* o4 `7 C* V+ `5 z9 `& J- O
for (;;) {! @( w9 o- y. T) `% h+ k
int current = get();+ ]4 |3 n$ F: \9 ~& S
int next = current + delta;* }9 K% ?6 ^+ _" W8 x
if (compareAndSet(current, next)): O# }+ Y- a8 b* u% r
return next;* u" T3 ]+ ~. _" W, f
} / T- f& f9 U/ U2 w }% t) H2 C6 O2 e; p9 L) f) K5 Y W
0 O d+ l- b5 u. _6 ] ( m, f. r( e0 @6 B& Bpublic String toString() { 2 r" j% n. e( a. { return Integer.toString(get()); $ O9 e7 x/ o: i3 K- U } % M3 G4 ?5 V3 N; m0 I* Z. Y+ y + Y: A# [7 b6 S0 G$ c' r $ Z" P6 W4 w- G. npublic int intValue() {: O+ B1 P9 ~5 S6 Q5 J4 n M
return get(); 8 o5 V9 h* B' g' A; d } ; b2 R' R1 o& Y' v1 F9 I# s! Y, |4 L4 i2 ?. `5 @4 @4 V( u4 [
" f* M: g) t4 Q, H" Gpublic long longValue() { ) H, H( a) r( @/ |- U return (long)get();6 |; I" e5 s$ @9 p, A, a6 Q2 G
} : E. M4 I) A, b0 O. b4 z4 s" Q+ _, I$ k- z2 [
& e: q1 Y1 a; n1 k6 R; Rpublic float floatValue() {4 c! [. q& L( w, Q( l8 [, c) a
return (float)get(); ! t. d( q4 g- N2 H+ N5 v8 j } k. @+ A' J c# z3 a) N) B' j( B, c! [9 A: m3 z
% a. D- @( ?) x9 @- n! l( Ipublic double doubleValue() {/ M9 V9 @4 e+ {- x" P+ l' _
return (double)get();* V/ x$ Z, @, C0 m; t' k+ {$ I
}# i* P' ]" C; h! Y! N
--------------------- ( Z0 ^/ ~- c' _/ R
作者:feyshine 3 Q5 [! d* @" x( Q来源:CSDN . s* K8 E; |+ [. W. S
2 J6 g/ ~/ I2 O z : P5 A+ D8 D1 I" {# ?* {4 R/ j: A c# ~) ~ e; f. ^' N