标题: AtomicInteger类 [打印本页] 作者: 杨利霞 时间: 2019-3-23 16:04 标题: AtomicInteger类 AtomicInteger类 3 [8 c1 y P+ n" G8 {( s7 \( S& w0 x0 l
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。- @1 n7 H$ H" ~
5 |* |, l( M z4 E
重点内容- ?. J* i( b$ \% n
- D0 r: m) N: e0 L j2 {+ B
属性: 8 L# K* J. ^" }# x4 A7 h/ bprivate static final Unsafe unsafe = Unsafe.getUnsafe();- }- Y) h- H# X3 ]
' _' u) O: h; S5 k5 K {private static final long valueOffset; / I9 b# l! ~0 d( W# O & I% T0 x. t) t6 O1 Q& `# Yprivate volatile int value; o- n/ B7 f" {. ^7 l& L! i
& v& h+ l0 |; _* ~! J0 D静态代码块:' R, p- J( i% p; e; W. B
static {; g4 [/ \7 `: v# ?
try {0 m* R' I$ Y1 K: E
valueOffset = unsafe.objectFieldOffset4 K j. p8 T/ |2 T/ z
(AtomicInteger.class.getDeclaredField("value"));6 t3 {* p* D2 C( H+ r& h# w) ~5 E; K
} catch (Exception ex) { throw new Error(ex); }' F7 h' ]* `( s6 r2 B; L
} 6 e2 [2 i) C( N0 E4 Y# x; B, A--------------------- 4 w4 R1 U2 H" @# x( v) r) B ~
构造方法2 L2 u" L! t+ N: d
public AtomicInteger() {$ d; g) y9 y8 ^. k0 Q; { c7 l
} 0 i- k9 E! O* I! S- \( ^" P# L/ H6 C. V! ]* d8 ]+ w
public AtomicInteger(int initialValue) {1 }3 {& I+ I7 z% T
value = initialValue; 7 V& g# Y$ X3 C- ~ }- z% {. t4 _ j
1 e; A/ `4 {" H3 w
对象方法:# \+ \7 [) p- P5 W
public final int get() { + d) |' \* \: B, q return value; ; G5 P; T0 I/ r } 5 ?( `& G: K% L " Y: y' x y5 x: ?2 Z 2 }* ]2 T1 S5 ~7 ~1 f$ ]9 cpublic final void set(int newValue) { 0 J& J% t! f [0 M' r value = newValue;$ N& C. l+ }' Y1 B
}; a1 b! k/ }+ v p% f
0 `) I9 G) y0 N. e- u% f+ c+ j5 K
public final void lazySet(int newValue) {# t$ o+ {3 A* H: ~3 D1 ?
unsafe.putOrderedInt(this, valueOffset, newValue);, K4 p* g( T0 Y1 t
} 2 {2 Q6 s5 y8 P5 q - {9 m+ r1 U0 a% Y' [ / G; E4 y1 Q9 Vpublic final int getAndSet(int newValue) { $ Q+ ]) q1 H6 g2 R4 C for (;;) {: R# `" p7 ` Y5 k. G$ b, w
int current = get(); I' D& x5 w) H: |" Y' E
if (compareAndSet(current, newValue))& K7 |; W9 k( s8 j1 `2 P) `: U
return current; 3 k. `4 W3 f" @7 L }/ }0 ]" M) S$ i2 S; Q d
}7 S9 ^8 Y3 y* d2 a
) O9 C+ O+ z+ c/ Z! o4 p
9 h6 ?1 t4 \2 h3 k, q4 m8 [0 B
public final boolean compareAndSet(int expect, int update) { , C. j7 c3 }/ x) G2 U F return unsafe.compareAndSwapInt(this, valueOffset, expect, update); ( N" D) K% w3 R2 p- ]1 O- N& B0 i+ }" J } $ g7 T, q, ~& n& \6 P; J, T0 t% m z$ M6 H; \
7 n* i7 d g) c7 a- s
public final boolean weakCompareAndSet(int expect, int update) { 7 Z- |( @- |; X return unsafe.compareAndSwapInt(this, valueOffset, expect, update);3 j/ Z$ A1 S: m2 z3 z
}3 }1 s) B( I* Z. R& f# y0 |! s
1 q3 R G. S8 N& @. J1 [
public final int getAndIncrement() {2 b' N0 D' S& W/ _. H7 n
for (;;) { . X" W: i/ Q: ] int current = get();+ k- j1 V0 R# T/ J: p V* ?* G) z7 P
int next = current + 1; " n. E7 W# q0 c: B) J* B* T: x if (compareAndSet(current, next)) " q+ \3 e' f" F' T a+ D return current; 0 M' ]( {2 s7 n! D } : ~$ k) E8 _* F% y. H }. A+ ?2 i4 Z! Y! \+ j
* f$ a7 G; Y5 W7 m# t% @
public final int getAndDecrement() {+ n+ m ?2 r% B( c9 w2 X
for (;;) {4 x5 e# L( L5 t' \/ M+ w! Y
int current = get();, K# P6 |* [2 w' ^
int next = current - 1;* J& v# G/ V( ?8 ]1 Z/ a
if (compareAndSet(current, next)) ! v0 G# M2 f/ d% }% r: d! _0 G return current; [% s- H# R. p& ~ }: x7 E* r! E) T: N
} 5 I( u7 j* p3 U* a4 P, C; r7 w2 P6 o
9 \! [/ T. }3 d* r' T% \. o. U6 t
public final int getAndAdd(int delta) { ; Z. }+ }) P, [: N0 t# k$ [! q for (;;) {2 n1 ^% f7 H o% L* c, S
int current = get();7 _8 K* ?+ }' }
int next = current + delta;% `4 {, I5 N) P: [
if (compareAndSet(current, next)); O4 ^7 q* s5 C9 a& G
return current;: ]9 }5 r! j/ l2 `! }8 s8 j3 M
} T: P" f; s5 d" F( |( A) z
} $ b* ]7 t) J6 E3 ?7 P ' Q. l4 d" {& V, R4 `' o& k ! v7 ^2 l, U8 \) spublic final int incrementAndGet() { / h1 T$ k4 c5 T. u' u4 h for (;;) { I, F) A- B; ~; e- R( W int current = get();5 S' O: e6 o( V( t
int next = current + 1; ! J4 G- e* x! z0 T& A/ | f2 w if (compareAndSet(current, next)) 1 R R# ~1 R* m1 x: c/ ` return next; # H) h- ^( U5 ^, `6 ~& i" a) W }1 z) E% L* e7 I" s, ~* H- k7 X
} 4 d3 i2 Z) n/ n W! P$ h. b# [ ]: l, W6 M1 |3 L
# A# } v: ]8 V( x2 {4 B
public final int decrementAndGet() { ; k9 s f: C. \) ] for (;;) { ; f5 q5 y; J# [% p( g$ L$ b8 s; K int current = get(); # m. {+ U% G) h" q! W int next = current - 1;3 c- W* O; C0 c8 X# S
if (compareAndSet(current, next))9 R* \' N6 B& s* T
return next;+ m. I' Y5 T+ ?) y! L& a6 x
}" }) B- H4 ~. @; t. W) {9 N, U) R; p
}: ]" Z: b8 K) f" e# t
) g+ F" n5 c3 L$ D1 Q+ e! l2 n( i/ g ` ' m( P' C$ K V) u1 y- Jpublic final int addAndGet(int delta) {' ^3 v& H) E1 r# G
for (;;) { " Q7 o% r& N1 v# K& r( i int current = get(); : E5 D! [, x ]& Z/ v- u6 Z3 g int next = current + delta; ! @4 ]# F6 h4 \ {& e$ Y" ~ \6 r4 u* A if (compareAndSet(current, next)). ~3 D( O" d( a. C2 j! B
return next; 6 }" D. f& {1 l }' K$ |0 T5 S' F. V, u
}0 D7 G) h& Y. o2 E4 _4 {- j: P
8 \4 l9 l, e; v4 @; H& i
9 O' M9 [ s3 x( H% G/ Z( ?
public String toString() { ) A; `6 [7 K/ F7 s% s return Integer.toString(get()); 1 r- o. L' e) U* W. l# s }" ]6 G# C( T5 z {
& b9 H3 O6 h6 k' y( e0 [
$ ~: S9 y: m. M0 ~- g% ~0 w; Ypublic int intValue() {+ k: [+ W4 j1 u7 p4 M q
return get(); 4 q! o1 q, Z2 W# I0 i }: M! u8 u- T- B/ c1 ?% Y& k4 k& L
: x9 O! Z* X: P. [. { ^/ H
3 }, L* f' z4 a2 w# k2 O
public long longValue() {7 P W7 K ]! c# K
return (long)get(); ) L# Q6 Z8 y o) Q } 9 x( {; B& ~/ R1 j& U, |+ C' T3 P* { : Q+ I/ w' z% f l; d7 z% y+ _& ^( `% o. k5 E
public float floatValue() { 4 p7 n* `* [" z- M+ y. O8 W return (float)get(); % G7 T3 o/ @$ F2 ^ }) \6 L$ u4 j3 M$ D2 f
* C+ C6 a; |/ ~% ]* P2 v2 W# ?* l7 ?7 E5 o" y/ g% c. r7 n, y
public double doubleValue() { 0 J7 s$ l* G0 h6 L return (double)get(); % x+ g% W6 I5 C1 e }2 I. f3 `2 G/ ^3 r$ g* T* ~/ I
--------------------- [! ?9 Q! Y1 S/ n
作者:feyshine % _" F( w! p& l7 L来源:CSDN 5 m1 \+ J) h( T, s! @2 W
$ j/ U9 `( ~9 x4 i# \: k+ b* v$ ^4 Z' y# h
9 l% B% a6 c1 @, L$ L) C: n
7 x0 Z* g& E2 I) y3 A. L
& a- C/ X+ R5 m) A0 J* ]- u7 Y