标题: AtomicInteger类 [打印本页] 作者: 杨利霞 时间: 2019-3-23 16:04 标题: AtomicInteger类 AtomicInteger类 - E* R: w% |# k" O) {$ N' o1 b0 T* f. g( U6 Z! F
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。, h y8 s2 Q, J% S) g. ~3 t2 y+ q
# p; {! i x, ~7 H% K+ w }
重点内容4 Y. e; r! ^8 Q7 z3 C
0 K+ o6 b4 E2 Z! S. q0 ^0 i O
属性: ' O- T6 d3 S$ `2 m- K* {0 `- B8 jprivate static final Unsafe unsafe = Unsafe.getUnsafe();6 z6 G+ s0 T! Q; z
0 n3 C8 D( M% a* \
private static final long valueOffset;& P: l/ G/ |& ?7 C, x( p2 Z
$ @) w; Y) }/ G" e7 u1 }
private volatile int value;. m# ]& q7 c) {# N
& V( M. `" D. g: g" Y6 h) ?7 l静态代码块:$ |) z5 }1 ]5 J' L4 v# h
static {% Q0 W/ R8 V2 N5 k
try {; F; _, W5 y* B1 ]6 c, ^; F
valueOffset = unsafe.objectFieldOffset2 [) e I2 L9 Z6 M/ |- g
(AtomicInteger.class.getDeclaredField("value")); . L# s; T* D g+ m: ], |( o4 X } catch (Exception ex) { throw new Error(ex); } 2 C% }! E; G7 k. K8 v } $ f. Z- Z: _3 R; L8 R V--------------------- 6 R+ ~0 e6 U3 Y) B& I0 p, {构造方法$ q% l8 N; F9 d$ \, H
public AtomicInteger() {% b! h4 n& |6 N" w& c7 ? P% Y
} $ @; o: a5 l0 J. s" z ! N* P& h- \; h" R0 A& e6 Rpublic AtomicInteger(int initialValue) { 4 H4 a2 X) s0 h* ?8 Y- i9 h value = initialValue; 9 R4 h+ |# C( z" p9 m( Z }6 w4 a9 ]* g) f$ s
, p; J6 z) |# g0 ?* a( P& ?对象方法:$ J! a5 Z. C* Q3 P$ c0 }) Q
public final int get() {8 @, I% Y' _" N9 b! r9 ?
return value; ! a0 T& J4 L2 l2 S0 `/ p1 L2 X }" t9 V1 @- ^" Z
# Y# {9 t# U" _; z3 s $ f7 i, m& J- X+ V1 Ppublic final void set(int newValue) {. S, |' ^/ {' ~
value = newValue; * ?4 y% t' @, L; s" n }% L: J' _3 {" ~0 b' W5 m
6 \" D! Y9 x; q& |/ B) h" F+ J6 X; [1 N# @4 o0 z8 n+ k
public final void lazySet(int newValue) { 7 f# J, o5 g* y/ t unsafe.putOrderedInt(this, valueOffset, newValue); 2 t) I5 \. b! Z- z& [7 L! \8 x } ( D9 N8 O4 F! r# c. F8 P' z" t" u& G7 A* g
/ M' t3 [+ F% ]4 v8 H, hpublic final int getAndSet(int newValue) {+ A% J9 i6 U) H8 ]+ h/ G/ r
for (;;) { 7 `! H' O: z# g4 i" E- `' I int current = get();: I W( P6 B, w7 q% _5 R
if (compareAndSet(current, newValue)) 3 K& M% n# H, `& Z return current;: x/ i; b" d7 ~2 L
} 4 f. ~, Y5 ?/ k8 O ~; k# q }9 d2 A( _. x( l0 N+ \7 d
5 O" h5 M* z; D8 d6 J; e; S; V, M) B- ^, o& y0 s
public final boolean compareAndSet(int expect, int update) { ; Z! g9 e6 @" |6 {* F return unsafe.compareAndSwapInt(this, valueOffset, expect, update);% G8 x4 n% V+ u D; t" j
}, K) t5 a- E) p
9 M w2 y6 o/ R- @
1 a& R! f3 X) ~7 U. \9 g: O' R8 Y& Q: Upublic final boolean weakCompareAndSet(int expect, int update) { . Q; a- j- p4 M/ E1 H return unsafe.compareAndSwapInt(this, valueOffset, expect, update);8 x R4 |: U1 J- V( A* d+ K
}# a+ y- S# k: Z) j0 J$ r, |+ L
! g% o/ I' y9 l8 w ^: S! Qpublic final int getAndIncrement() {. z- @; @- ]4 u& w
for (;;) {/ h. `: u" G+ M' }
int current = get();+ Z; b7 X; S% q# x7 [- G$ d5 \1 ]
int next = current + 1;4 W- Y, m2 k0 A& j3 k- m+ Z
if (compareAndSet(current, next)) , t4 R' [" x/ q4 [. r' O% H% l return current; ( H$ f9 ?/ w3 T+ _6 q- V }4 j# D4 _; T: H4 i% u
} $ V! ]# W2 V: Z# M+ S& [ ?" i7 G' m2 J
public final int getAndDecrement() { " N, x# q% A( l; N8 d for (;;) {- Q' E% g$ u8 D" e0 A9 G
int current = get();0 i0 b6 V) T- K6 f5 D0 W% S- r
int next = current - 1;5 {( C, B' Q1 { i) j1 W3 W
if (compareAndSet(current, next))' F' r9 A7 _) t
return current; . i3 W( Q9 J; E! k }! `$ q) M! M L3 S8 g
}1 V; ]; k* Q* n K
, ^, A6 H+ |( @6 s) Y
N1 l* ]: f2 j8 Jpublic final int getAndAdd(int delta) { ( P, ^4 ~- o; y" p% T# x# K for (;;) {6 C: z3 v7 T7 y( T5 ]! W
int current = get();1 R) } j+ P9 `+ p3 z9 j
int next = current + delta;: ]3 _9 w/ F' ~" d& m) G
if (compareAndSet(current, next))5 J' e& m% N# a2 [- b0 m, T
return current;0 r; E# q& p2 `! x8 U+ [' }
} 8 D' c' W6 H7 L$ z }( N" d' ^" B: k8 x
% w+ V k4 h# j8 G
; N# c: [8 e5 j+ s! O2 ~3 p; Apublic final int incrementAndGet() { 4 M5 _, e6 _/ E7 b3 r8 Q for (;;) { 7 f/ o' @! a2 G( c+ F; E int current = get();2 V% S* ?1 j/ b( [( Z
int next = current + 1; - b4 x, R+ P, K, j0 D if (compareAndSet(current, next)) # _' Q# N& @2 E+ F0 B return next;) J; W! w" E) `' h# @3 Z3 ^# i
} y, I) w: v, k% l; H) v }2 |' r+ b# z: q+ h8 e& E* ?' j6 p
, d8 t% U0 m5 k8 g3 X9 o* V
p% N$ }! G4 C5 q! ^public final int decrementAndGet() { 3 I1 E1 B' z1 K3 Z for (;;) {4 V* y1 L& ^; d" w
int current = get(); 3 _7 U, m' ^! B% E: B6 \ int next = current - 1;' p8 p$ ^& b4 s9 C
if (compareAndSet(current, next)) ' W6 ^ I; o. A2 `6 ?. o return next; ) `, n$ d' M ~% v2 B9 N6 Z1 @: v- l } e& R4 j: J) ?) F } . c4 t+ d$ Z0 D8 s! H, F; Y3 g' e, S- z" [
0 V* l' I, ?0 w) F/ ^$ A, u: k
public final int addAndGet(int delta) {. g4 w# |1 ?+ ^, q4 ]. P0 o
for (;;) {/ E0 {+ G- f% l0 t% w
int current = get();0 m" T/ Y+ T+ U. J# a$ [
int next = current + delta; - \! G1 {" X. V+ h: ~9 Z if (compareAndSet(current, next)) $ y0 c7 O( b7 ~3 z& Y8 T. U! E2 W, I return next;$ f5 u" P f5 o' q( F
} . n6 g# A0 _/ o! `$ J( y7 T }, A: `0 O9 b: o" j# y
' @% j) U0 ~& y2 N, o$ P3 U
) ^4 s4 v' P2 H% b: l0 v
public String toString() { ) j6 A' P+ E# p0 M/ @ O return Integer.toString(get());* ^& |& _0 |, {$ z y$ F
}- y, H9 C; Z2 {. v
% \ L6 L! D( z) j% A5 R; R% z9 a, B0 f- B
public int intValue() { + u7 g: n6 }( L* u& a) [5 Z" P7 [3 b return get(); 2 a& ~1 v* n# y8 c8 S$ V& E" S2 O }6 o+ b0 H' [9 c2 X7 q2 A
( Y! v4 z* x6 S3 M6 r
* m6 l2 s5 o' I
public long longValue() { ( F& N' D" H D |7 V return (long)get();7 x& n3 Y. N: W) h2 g- R. A
}! Q* s, R& j3 P$ S3 m0 ~
1 p' A# i1 x4 [3 C
5 x' \0 l/ ~- u" {0 p1 a# m
public float floatValue() { + E. y* h( L. r6 h: m return (float)get(); 0 I! l( k# ?! G2 R" s$ }, r } ) K# Y7 g4 W0 y+ h& i# e0 I: Z ) U2 J& A' z u9 }4 Z- R" K( h % `" Z% R1 v/ p, u+ e% t2 |% Fpublic double doubleValue() {& F! H! t B1 X. w
return (double)get();/ I$ J" v( ?2 P- R0 ^3 v4 l1 N
} ! I; a1 |+ L( C1 r--------------------- , d8 i7 @8 G1 C; B8 F( Z& A作者:feyshine " C3 ~8 o H9 Y
来源:CSDN ! B b" }$ g: C! y/ s
( ]4 `3 D8 c9 s6 ?9 N' P4 c# p
& B+ Z& ^( ~ m0 [) N+ v+ W , c* S+ A( M- `9 o; }' Q+ R2 P / L7 G+ q; Q4 D$ S . _% Y8 S- B0 `1 s( Q9 k% D3 n