AtomicInteger类' c. B8 ]0 C- b( |
# Z& O1 i" v2 U" P
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 ' v* ]4 H0 Z7 I6 S. d2 e $ K8 N9 ^# }+ F6 v重点内容6 s# t" r; f+ ]3 |
+ i- V k& r6 M$ v' l1 @6 y+ k2 H: i属性: # h) l; g9 D# Zprivate static final Unsafe unsafe = Unsafe.getUnsafe(); ( X) L# G; o" ^2 G- B7 V1 ~$ J: l. R0 ?% x
private static final long valueOffset;3 g* h+ y$ P- J4 ^
5 m1 X8 {$ l4 ^/ C4 A" Y/ K- N" Jprivate volatile int value; ( p& u+ ]( X% C$ J0 a2 u4 {, L& W( _9 ^. X7 w+ u% {
静态代码块: 8 S4 o+ ^" i2 a, l6 Estatic { % G8 ~4 |( N( b5 M try { 3 a- [5 `$ |$ p6 k valueOffset = unsafe.objectFieldOffset * j& Y% P7 E: \9 O* t (AtomicInteger.class.getDeclaredField("value")); / V. a; e1 |0 a, w" x: L } catch (Exception ex) { throw new Error(ex); } 1 @' l) B N' v; M; L% {8 Y }% r _+ ]5 I1 U3 O' M* C; r
--------------------- - v" \ j7 F' ?2 N构造方法 / s, G* c3 M( H) `5 L) `+ ~4 H h$ Rpublic AtomicInteger() { / l) \% d1 ]4 V3 U } % F: x, p4 y3 S4 @2 C6 t' p, P% o) R: s/ u! P" E2 E1 b
public AtomicInteger(int initialValue) {/ W6 u- G# X' f
value = initialValue; / {) ~) B0 G- X2 t }, e9 N" ^( o. H7 d
2 ]4 ?$ b& }) m$ T7 q2 t" B, h对象方法: % k3 |3 V* c0 m* u: Z( r! m" w public final int get() { 0 `/ K* [: c o+ e" Y6 N return value;! M" D8 ^4 B! U* A- U0 b0 t
}# t; v% b) r! Z" e' f
9 s9 o" \* Q* H+ G* j; f
, v) _4 [; X% |public final void set(int newValue) {9 `- _. g1 [6 z) c# B" }
value = newValue; + s" O6 O* Q( K+ }% n }) p- `" p0 X' @! Q! @9 h
4 b7 M) B* _! b; @9 K K7 \ * m* O: H% n; W: _: }1 ^public final void lazySet(int newValue) {8 k3 ?! j ^2 T0 z# D& g y
unsafe.putOrderedInt(this, valueOffset, newValue);7 v( a- v- V! a# } C; m
}" P7 j# l; h% u' ~) Z! Y
) F2 S! u' y- E, \1 ^
' Y3 C3 S L' P
public final int getAndSet(int newValue) { $ q+ [4 i4 A5 u for (;;) {# v c! M( v0 l, M* ^+ G
int current = get();) G1 _2 ^3 j0 G3 S4 q4 L8 Y
if (compareAndSet(current, newValue)), q$ k) n5 u0 F3 T
return current;$ h! y6 I. N' Y$ r
}9 g3 l3 B3 Q( Y8 F# h& n7 }
} W3 S% d6 ^. s* Y9 |# Y' Z g" {: J' f/ i
- J1 e4 [4 u7 m' Cpublic final boolean compareAndSet(int expect, int update) { 6 n& S# ~7 R' w% A return unsafe.compareAndSwapInt(this, valueOffset, expect, update); " R7 h$ S7 w3 D E& A } 1 J1 k5 s8 O# q* p# L d- d' Y7 q) p& A1 ~
* U$ U: D9 ~6 \* f, l# |
public final boolean weakCompareAndSet(int expect, int update) { 4 L* h. Q# s% r9 M- x5 ^- g return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 6 g$ a' N5 N3 n$ G } . j+ t& R; q4 |( m% z, ]* z @$ o- _/ z
public final int getAndIncrement() {& g1 e* n6 p! D# U* M+ u+ m. t
for (;;) { q( Q! E6 r; h
int current = get(); 6 H* i" n/ c& V, t x+ F! @$ e0 d int next = current + 1; 8 ?! x6 i7 Z$ P# L) f" { if (compareAndSet(current, next))* ~6 C8 x( R1 T& v& ]* | r% @
return current;& M0 C- B, z1 X; q2 f7 w( W9 j2 j1 a
} 2 l; T( c; m& j6 A# c }* `1 I1 a- C7 H) S; R8 o4 k/ U
/ b8 b6 i3 F2 ]- @
public final int getAndDecrement() {) F. w5 X& ~6 R$ V) \
for (;;) { 1 a/ \- z0 H6 t int current = get();4 B( ^ u/ h) M( s2 |" a6 G9 E
int next = current - 1; ) m, r" P! c/ ^! u- u; y; P if (compareAndSet(current, next)) 5 D1 R- v' ~4 X return current;+ L+ s. J; L# T7 `$ T& e! h
} + I$ V- K# ^5 d0 } } ! b5 c6 |% Q/ H5 ?8 b7 P " d4 n2 z3 f/ [/ a- `+ T( a& v2 ]+ D8 U0 I. h8 \$ X+ u# V
public final int getAndAdd(int delta) { & F5 T1 I- f1 r$ p# } for (;;) {. d; _( `0 o/ N3 v
int current = get(); ~3 d8 Q0 j3 I: p' H
int next = current + delta;9 D4 J8 u% c6 @- L) ?3 N
if (compareAndSet(current, next)) : p1 g- R5 c3 B Z return current;; r4 N* ^: |. y) t( \' v* @2 G
} / k, H( E! Q5 e+ m }7 T2 e2 @$ H# P2 Q4 i
) k* H: e# B$ V6 K* f ' X$ x& U8 g6 @0 Dpublic final int incrementAndGet() {8 n) j( l& f$ z5 r( d6 w
for (;;) { 1 M5 M( l0 c7 n2 X; f, c: z; M int current = get();, M8 |: f9 ]- }: V; G& A
int next = current + 1;# _- e) b6 H' l) y% M9 N
if (compareAndSet(current, next)) 0 {( K: w5 ^0 ? return next;3 k/ n5 I8 p5 }' P; X6 \# O, h; U
}( T% M. D; h8 s3 C# t
}7 I' ` H' P5 b" u: x$ E6 v/ Q
' Y9 r$ ?: f8 f' V7 M( H: w7 E1 K; z2 l0 x3 ?4 z: O; a
public final int decrementAndGet() { - _2 @$ t: R- e a1 t for (;;) {8 {$ K o+ N0 j" w! T) K ]
int current = get();( `5 ], f5 u B* O
int next = current - 1; {0 M7 _: W1 w. U if (compareAndSet(current, next)) ( y8 T0 Z6 X/ B return next;* A( B( M3 W& k' v
}! ~( E% @5 T9 X5 c4 Q2 a
}, B! j5 y: p6 `. r. k9 d. s: \" d0 T
. F) A0 g: K! [& f, w9 i/ R' J" f) i# B. L
public final int addAndGet(int delta) {9 L V5 t: N% @ s2 m3 P
for (;;) { * k, h8 r% v1 \# n m3 m- n int current = get(); 1 [/ x- M _% [: ?( q int next = current + delta;, a* B" ~$ Y8 C" W0 K4 J
if (compareAndSet(current, next))- \2 H* i3 L9 d
return next; . k* [6 a$ g0 S" i0 M, S } / S# p+ ?; d- y' X7 i% F! B6 ~ } ' p N( G" P/ i: t* b" h8 b2 H8 a7 n" \2 [2 M/ j
4 X% {5 E) t% D& R1 v- upublic String toString() {7 G/ o; Q" N& M, g, E
return Integer.toString(get()); 6 w) s; @9 c4 @' M; w! s } : |1 i. H5 \+ |4 u; V0 W* v% Y0 M. o5 a6 i/ }
2 u7 r; M3 V7 N" `public int intValue() {( |3 k# i5 n, v- Q7 Y
return get(); 3 ^5 j! a/ N; K) K3 {+ s9 N }1 u% S, R; X6 L# V6 b: I
" `& E; Z9 ?' ^: {- A {' M+ x8 |9 G% L& h* j/ l& z; `
public long longValue() {. g- B+ U: X. Q( ?5 z
return (long)get();! h& I0 D3 K. I% h3 \8 l1 ` v
} 5 Y$ y, v3 s7 m7 z/ {( S2 ^3 B2 r' e: q$ w' y( e3 T' x3 r
8 n7 |( a _: X8 M
public float floatValue() {: ]) J/ a# X( v4 @/ ]- E& C
return (float)get(); 2 D% l! o0 T" \' x+ A } 6 c* \* y* D) o 5 @7 K8 t! {" y# m# m9 J' Q ) [: o9 k4 t f/ i2 }public double doubleValue() {6 K0 j8 S1 h1 W7 S" ^
return (double)get();* N. y( O! x0 m7 f0 L3 E# N7 G& N3 @
}8 V0 i. c1 R& \6 L1 a1 m
--------------------- & A o3 R9 |, ?1 G4 Q作者:feyshine 4 a$ A/ z- e. _0 \+ Z* n
来源:CSDN # N0 w/ H6 l( s! q! d7 @
% h* a+ m; v7 Y4 h, K6 c2 z7 I ; @) L' W) |5 W4 p4 e7 _' |% t/ D; c, X7 r: m! \+ f
$ E: X* L! x7 d1 q