标题: AtomicInteger类 [打印本页] 作者: 杨利霞 时间: 2019-3-23 16:04 标题: AtomicInteger类 AtomicInteger类 / F, n6 a5 `9 y+ q/ T, E# \9 _6 I4 |+ L7 Q7 S$ h x3 r" e5 m
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 y" A+ ~5 l, j+ r' C9 U) |0 K/ w6 u
: _" W' I5 ~2 s) p3 d9 t
重点内容 6 @) _+ b: S, S' U0 Q- @7 r " t( u, t5 i: ]+ |3 i属性:6 D3 g1 A5 h8 _6 R1 F
private static final Unsafe unsafe = Unsafe.getUnsafe();4 |+ q- S, ^1 @. G: A( X" X
! k& {$ E6 I P8 |4 l
private static final long valueOffset;: l/ U# I; Y+ C, L8 V9 w
: G5 g9 c# Z( Q) Z# E& U1 R3 b
private volatile int value;2 p1 M4 U) Q4 G1 _, m8 V7 {
5 q) ?9 U+ Y( [0 ^. Y静态代码块:) @5 ]4 I, P9 v* f. t; X
static { - v4 r0 [# L m6 L8 D; B c try { I6 Z, T+ q8 t9 N6 | valueOffset = unsafe.objectFieldOffset2 T& M. V9 f2 f. \4 e% k: p% ?! f
(AtomicInteger.class.getDeclaredField("value"));/ W0 I O- G8 x+ n" h$ Q
} catch (Exception ex) { throw new Error(ex); } " `1 s# _, e1 s( n/ \ } - D) [6 B& r' b" C--------------------- / a# Y" h( S! c, B7 C1 s+ ?
构造方法( s" \" y% j: L$ v1 O2 A+ W0 g W, M
public AtomicInteger() { ) A8 U d$ a1 f7 g5 v } % h2 a; C/ i3 h 4 S" w4 s2 |. l! T0 \; Upublic AtomicInteger(int initialValue) {3 ~; N. P" W0 Z' n3 s2 m* C
value = initialValue;- v& O7 Y2 i2 B
}1 I! j% L0 _! k* R$ ?$ {# d7 D$ f
8 \& h0 C p# s4 k9 D m! v, E对象方法:( ]* D/ u" k' s- ^
public final int get() { ) q( q' |3 a# o; Z4 G( O# Y return value;& B8 `% i2 d a5 t7 Y+ P& W
} 8 S% D8 x! [+ V4 m: e+ B3 [* }0 O5 q4 a6 v. Z0 _& ~ x
) P+ p0 M/ c7 \5 Ipublic final void set(int newValue) {1 {' O4 k9 s4 f# U( w: U9 a
value = newValue;6 w/ }9 o/ b# {. E- H
} ( _0 i3 C) D! y* d% X- H0 H* V d/ X* a# K) V) E" _, j. ]
0 Q1 B7 P0 D) U! x
public final void lazySet(int newValue) { E+ }: o0 H* S2 d0 q
unsafe.putOrderedInt(this, valueOffset, newValue);/ p6 j: f1 {* O, l
}1 T, L& b. u @& g. K# x
; D* P C' |0 h/ C1 E+ ]' R
, n" y5 S/ h# E+ F& W* x& r* _public final int getAndSet(int newValue) { 8 w# C4 l( v4 D for (;;) { ( h- p- v$ N: q0 |0 J5 I* Q int current = get();) _- s o: L" N) _, r6 e% I
if (compareAndSet(current, newValue))) ~) Y: h5 _. c" X& r$ B
return current; 1 f5 k/ d [( @& ?6 k } / `; s4 g' A0 F& O; p' z }5 M. t7 q- v& L( V: `/ _. y, o
/ V. O- u d/ @# F. m" b
8 e1 o7 Q% p3 q; q, @9 |; f" fpublic final boolean compareAndSet(int expect, int update) { 4 W9 \2 Q/ l3 I' Z5 `$ z/ r! N return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ) @" C c: T- G9 G1 ~ } 4 N" `. p( c$ a4 N" }# Z7 @8 l3 d3 f3 ~! a
4 V/ r4 C) S# C, c8 c
public final boolean weakCompareAndSet(int expect, int update) { ! u; g. w# m2 Y9 I& t return unsafe.compareAndSwapInt(this, valueOffset, expect, update); " m: U P3 V# m6 l/ p2 i/ j }4 m# L$ R2 m2 T/ U. p! G. B( N
8 J# I6 @. y+ ]
public final int getAndIncrement() {0 a" e8 ~1 x+ c$ q
for (;;) { * b! T9 U- w: Y4 C$ J int current = get(); 8 x& H$ ]" L8 ^5 V( g* Q int next = current + 1; $ m, X: I, h; G) _ if (compareAndSet(current, next))3 v( q3 i; c. H4 P2 k9 P8 z5 e7 C
return current; * h* D* y8 I9 y( K# U } c7 S% `2 l y } 2 `+ _, @3 t; n& b" `: R+ q/ L ( u3 D- ^# x0 \! x$ j; \0 Lpublic final int getAndDecrement() {$ l2 N; c: \/ k5 C; u# O
for (;;) {1 n- o4 v K, n- W% P% Z/ r
int current = get();* c8 d3 K" I9 H6 G
int next = current - 1;: r$ Q* [) H& ?1 h
if (compareAndSet(current, next))2 f+ k/ {! M: S( t Y y" _8 L1 G, z
return current; 5 u/ z/ L/ C- M& t/ g4 R } / E' g$ J% P# P3 c8 V, c) t }) \7 Z K8 n5 z" W% h
- e; q1 R0 u# V2 C 2 G# _7 f" y4 Ipublic final int getAndAdd(int delta) {0 T1 E4 F+ Q2 K
for (;;) {& B* n" ]$ \+ P, x8 Q
int current = get();; D8 s! \, `/ U, c8 v3 ~; J
int next = current + delta;! [4 ]0 k# g0 v5 V4 ]: I" B1 J
if (compareAndSet(current, next)) ) {+ i, K. z6 Z* B( P return current; 3 i* ]6 X% e' ^ V( R& f" L! Y }' [/ m; S8 p( l+ a# Z1 C) ] }% i
} ; ]9 Z8 |4 ]) D8 R. _7 { ! `: h5 _/ B' T8 O & b( \+ J5 O+ [public final int incrementAndGet() { 7 S/ S/ m7 }/ {7 { for (;;) { 2 T1 F9 c+ H- O int current = get(); % O8 Z6 X. Q! O' S- S1 k int next = current + 1;- A9 n+ |" _' Z
if (compareAndSet(current, next))" G# ~; \7 X* [' ]; I, |. i8 W" b b
return next;" o2 s* V7 l# Y+ b% C
}0 j! O6 ]8 q2 s; W0 B, {# Z
} , A2 u4 r2 G+ e* l$ V/ k" M$ ^; t$ N( g
* E' |% h: |7 ?8 d; fpublic final int decrementAndGet() {/ Y+ t/ {! \' _3 J
for (;;) {3 c6 _; {& k" l4 Z3 V
int current = get(); 0 U7 s/ t/ u0 Q, }1 t" t int next = current - 1;' q5 n. j) X5 D( }3 ~- ~: y% y
if (compareAndSet(current, next)) a/ T Q; h3 i5 S3 ?
return next;3 B3 `+ N, h; `& q5 E; y1 |
}" P# v- @% `+ s% x2 S
}; H: S) t; x9 S9 o7 w& B5 R& f
, g7 J+ j* w3 Z. n5 e* R# r; m, }3 x P [+ K7 T2 U4 [7 N
public final int addAndGet(int delta) {: Q4 {) q% X* k- `. C4 I, W
for (;;) {& z1 H5 V. {% f' I! B4 T" O
int current = get(); ' }$ T( K4 m+ w" d0 E int next = current + delta; $ U8 z9 v7 \8 y9 y# E0 Z if (compareAndSet(current, next))8 T! t/ S, H; Q j
return next; * U y% W5 Y$ c/ X: e } s3 b$ ~' f0 M5 O3 c
}& U3 k4 C U. Z$ n8 i. |; Q
; Q; w. y2 c0 Z. I5 s" `6 x
3 f0 B8 |: j$ X: V* X6 P9 f& Fpublic String toString() { 5 N% E8 f1 Z7 {6 f7 v1 H* M' t return Integer.toString(get());0 P4 e$ I- h8 P! R" ]8 [& x
}' r( c: D" C, o5 q, C
# J) @' d# m6 k& B3 W% S& o8 `; N' X% d
public int intValue() {) E5 t. r1 ]% t
return get();7 ]8 t+ t# Z; b/ ~
} : P3 f! r9 t* @$ t, l" m0 {/ `% }3 k4 @' @3 m+ F" @# U; t
) v. {0 ?7 |1 m4 ?# ^9 Qpublic long longValue() {$ p4 G/ [( z% j" T
return (long)get(); 1 n _" ]( G4 |! r+ G }5 L) x; d, n8 W! z8 o
+ V* T! \) _/ m$ P+ r3 m" V$ d C. Y( u & h4 G' i- F' r; x Upublic float floatValue() {: a3 E' U6 {( G6 U* H
return (float)get();, d# M8 M% B6 V1 i# j O7 w
}( N- q) b. U- b
+ H( D, h) [1 ?3 E, B' Z& ]2 Q
' M% _8 `5 A1 K `; G7 V" r( Npublic double doubleValue() { 5 \1 _/ q7 [+ d' x return (double)get();3 _6 R" K5 d2 N9 X
} * A+ W' I" V6 X! F4 r--------------------- 4 b: y+ a; P+ s" `) n/ @
作者:feyshine 1 o+ W2 D r, G) B
来源:CSDN / _+ C( L) f8 g5 Q+ b' B" ?# ^. I) { 6 l6 R! F+ e) n# y+ _3 U. Z; j+ z& m* Y$ C% b) H9 ~) R! f
7 J/ f: j6 x; c7 R e ) E3 ]0 \/ _( e; ?" \6 m' U1 p1 ?6 o5 X ) C4 T( l5 U4 [