AtomicInteger类 : s, p f; L$ P" k- m, ^* D8 \ ; K' e3 r3 J2 p+ R+ X这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。& `. A: v+ ?$ k8 j0 F
* A% R) w2 y- Q1 o/ l- A, H
重点内容% |! c& d T8 `1 S5 p
j5 b* ^2 U8 I; p0 L/ r; Y9 l' t
属性:8 N$ V( w% Y, ^3 ?$ ^" W
private static final Unsafe unsafe = Unsafe.getUnsafe(); 7 G; h) W: M' E0 L0 i S/ P* b: v9 u, iprivate static final long valueOffset; / ?2 j3 z( D1 i: Q" O: f# F . C' @4 q3 Q! A9 f1 I: k3 }' e0 e6 Uprivate volatile int value;! y) [6 B: `) [
/ D. F9 o. F0 a" I7 E& {
静态代码块: , g/ u4 E. E$ L, R6 ?6 Ystatic {7 m, ?& Y2 V) o" C O5 `
try {3 L0 y; {( M. H# H8 `
valueOffset = unsafe.objectFieldOffset 9 X3 m5 Y7 a2 p T; t9 m (AtomicInteger.class.getDeclaredField("value"));# H6 G) t* q4 ?4 k) L
} catch (Exception ex) { throw new Error(ex); }$ C! c- @5 T3 v* P4 n+ \
}/ ]6 q& h3 G; u$ u9 Z% D. L& O
--------------------- , r- s/ N* R# A; ] M* L4 n构造方法 . Y* V0 y6 I% K0 a. hpublic AtomicInteger() { $ V( T7 A) f. D% H( \ }! W8 r1 f: {5 W# b) D- @. r
) H# p" t; v) b. y2 K; B4 b
public AtomicInteger(int initialValue) {1 Y- R0 J! n; \; O
value = initialValue; ; x/ U+ J9 W, s1 B$ A }" ]8 d9 i& ?' `% a) z
) \* z; ?5 P9 |. Z( e! w; r对象方法: / |% U8 d8 `" J" L( F4 P public final int get() {. w! ]6 j2 K M# o) q+ O
return value; / T: }! q+ P; E) F- Z } - _ E+ {) W5 g: j! x' f# c$ P8 }% W + v/ ~( F# o6 p- W( h# F$ t& p5 r$ B: U8 ]2 E) ^8 `4 N
public final void set(int newValue) {% k& N2 J* ]( S! ]" R6 Q# w% @
value = newValue; }9 D, K2 g r9 [1 C1 ^0 H9 C' K } 2 u' k' N# M( [- h( Z6 Z7 H- ^/ [# a# f6 Z! K0 F
& u) u3 \# P- k+ Q( N4 O. [% L3 r
public final void lazySet(int newValue) { ' E" W0 k9 S8 b. d' d# `& J# ]8 Z) l unsafe.putOrderedInt(this, valueOffset, newValue);8 p# T( P9 Q" H E
}% \5 U2 l! F# | p; F3 G
b0 [/ f+ ~4 U) T0 s9 x$ s
7 e. |- u3 n+ c Y5 k- F+ Ppublic final int getAndSet(int newValue) { 9 w0 @! V8 e/ ^- M) A7 \ for (;;) { 2 U1 Y3 Z! U- S; v& c int current = get();9 I6 `$ _# I1 x) h" T k8 |0 S: f
if (compareAndSet(current, newValue)) ) T$ q7 y- z+ [& w& c# q- S return current;7 m; o- Z- W. d6 ]
} j8 l7 M6 L* K2 r. G/ ~ }& @5 l' q; o1 ]+ t# I7 M
+ v9 b: E" s/ E# O! Z 5 v8 g# N' H. E+ G2 _3 j/ m; ipublic final boolean compareAndSet(int expect, int update) {1 J' f0 @& Q% X; W) K" t
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);0 }; [) Q9 Q3 Q2 j, [
} " D; y6 }% z& ]9 O6 U$ v2 s" C# o9 I) z3 N3 Q4 c
% s; q. e2 ]$ m+ A! k& i! L+ xpublic final boolean weakCompareAndSet(int expect, int update) { 7 P0 q# V6 A: Q9 c" J return unsafe.compareAndSwapInt(this, valueOffset, expect, update); - r* `9 P% u0 t) ?8 U } ; b& N1 g- v) _& R/ h1 J+ |7 l* n; P& w* F% Q' j! M, `
public final int getAndIncrement() { : _) E+ A7 K2 e$ Q9 v: k( G for (;;) {6 x: ]2 p$ f! @# A9 H! L
int current = get();3 U! L! U$ p! _. ~2 z' |
int next = current + 1; " ]; }: D/ z5 a8 U. R* U% D, n" S, o h if (compareAndSet(current, next))- ]1 n* n( q8 g+ u
return current; 8 U9 t4 ]7 ~# S+ D8 i }; Y1 Q! [$ |) q) f( Q2 {2 j
}$ R8 ]' C: ~' I2 _) U0 i
! t# Q# l9 J, T. Bpublic final int getAndDecrement() { . ~+ c- ~7 u1 c; M/ m" l# N# B" ^+ h! V for (;;) { i9 |7 h( f" V
int current = get(); # _+ i' o* J7 g) D+ _ int next = current - 1;+ J! ^' |6 C$ W. @ c
if (compareAndSet(current, next)) 9 o5 K! `7 ?, \6 v# ?% J) k3 m4 m return current; ! [) m3 ? m" S1 V% h } " `# e) e# x8 E1 L. @3 Q }3 a8 \) f$ u% J+ L7 Z) g, {
{9 O8 s b' `+ ]) ]
; B/ ~. k6 b7 p- [1 J6 Npublic final int getAndAdd(int delta) {; N$ x i l" ?3 ?- R
for (;;) {* J4 L) X, D" J1 \+ t8 |/ J
int current = get();4 s5 g7 r) l! k5 N
int next = current + delta;# e s/ y/ S& W" {
if (compareAndSet(current, next)) 4 d+ T0 H7 H7 T: g return current;. D& x" y$ B6 g7 q5 o8 y" H
}( ~0 |9 u1 J9 w! X; r: R& Q
}0 o+ k" J2 @4 [' I7 D* }2 H
- { e/ z, _7 h4 j) z# ? , j" T- ^$ F$ D( c8 x& ~public final int incrementAndGet() {) Q0 N; {* Q# l8 f4 x: B: ]6 b6 p# T
for (;;) { ; [$ A( q8 N6 J0 x( r8 [) z+ Y- l* n int current = get(); $ N/ ~2 H6 b( W# n: z) D2 b9 S int next = current + 1; 3 m& {' L: M/ S: Y' M; z% i if (compareAndSet(current, next))- ^( {! }9 g f
return next; ! `1 z* |, `* n+ r) J" W( D } - g3 l; D$ ]( M$ P) B+ O* ] } & w9 d0 i2 s2 j( w5 }) K0 d + r1 L' ]3 w/ S: o- i) c. o1 n9 Y) C0 g# ?# [
public final int decrementAndGet() {+ ]+ X6 w$ H' B) m
for (;;) { C- ^( w" D/ ?' Z% `# x
int current = get();' B3 ]2 m$ A# g2 X
int next = current - 1;7 f& A; k, I' B T6 k+ m1 n
if (compareAndSet(current, next)) 1 t, X5 [* u i7 A+ L: O( M, q return next; 8 q* c3 A. U- v1 n+ L$ D }; Y# c) P, J1 z7 f
}& b( W P$ y, o
|* |% T9 N4 d . t, V0 l! H% j: r$ t; T% kpublic final int addAndGet(int delta) {$ L6 L, t& B5 J
for (;;) {& f) g0 l' T/ m; s+ e. \) e
int current = get(); 0 T/ s2 T4 [3 V0 P( G: H6 A int next = current + delta; 4 }& H5 }' ~3 G if (compareAndSet(current, next))& a' v+ v, h, q: M
return next;* Y7 p5 X1 q/ s5 J
} 6 `( `5 c) K; h0 n6 H7 V2 L4 @ } * q+ e4 G8 O: w/ m- _; h1 ]# F9 V W
# P- ^9 F o$ l! A. J
public String toString() { 0 H9 l1 @4 P) ~( v# f& l! C return Integer.toString(get());. d# I Q' \/ w0 w8 q9 q0 F
}' V* d7 v8 [8 t' b# \9 ^
( v* ]+ L$ h A+ J" I, L" a7 E" x* x2 r2 T5 H# r% J8 ~
public int intValue() { " ]% j$ ~! m* L return get(); / S- E5 g: B$ U( w- c: | } 6 @* s- y; U: t- x! R. z6 A% a# C
0 u T4 m% e3 u; |
public long longValue() { + F {5 K* D# ~5 `) C return (long)get(); ) p2 m1 y: ~/ W" C2 U } 2 @8 j: O5 p8 n: J& Z7 b0 [, ^) c 6 @8 ]+ h; @/ D8 L) a 7 R: B% B% x- n8 R# ]public float floatValue() {" C# v, |, n9 ^ `1 h& `- p- s% K
return (float)get(); 0 D/ L2 K y7 J+ [ } 3 D" V- j e" q- ]( b6 T * z- r# e: {, Z- ~& w 2 }1 e9 f2 {) K& Z3 w" ?6 U1 j' ypublic double doubleValue() {: t! K# @0 D2 p* G5 }$ w! A6 q! u9 T
return (double)get(); ~4 Y% e* N0 [1 K. w }" C2 t7 Q; a. J) b% E
--------------------- 4 g( ~' b' X9 V6 P作者:feyshine 9 l7 \+ k# i! s来源:CSDN - E% B& C. }% r4 s- \
/ p& h$ a" h$ Q+ e Z: P. B. ?/ D Y8 W, E( j% w* {1 S5 b0 Y4 x
! n+ v# h5 g R( I- ], A