AtomicInteger类 / `( ]+ R+ {( C' y e # O$ \ @4 `" l- Y; ]这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。% T6 T% g" @3 \' W% E y
7 e v5 ]# F$ a: x$ `* l0 @6 x
重点内容' O+ x2 {% b# q- X" r7 @
8 r' q8 {5 Y6 g! W' J! z属性:1 O/ E2 @, U2 j! ?
private static final Unsafe unsafe = Unsafe.getUnsafe();; G; Z8 x8 j& h5 u) g
8 G% E, ^& g1 |& xprivate static final long valueOffset;8 ~8 P. c7 n, g* q
: q r$ \9 d8 o5 i+ vprivate volatile int value;$ A) g, L" t% J" A
j7 B1 b- C+ ]6 r* K
静态代码块: . V: N! f7 u& |static { % B6 g& Y* H0 ?; Z- O try { , H U1 ^* N4 v* b+ b valueOffset = unsafe.objectFieldOffset# @# `0 Z- A& w
(AtomicInteger.class.getDeclaredField("value")); 1 D. l4 A9 S9 _' E$ X } catch (Exception ex) { throw new Error(ex); } ' T" h9 P3 d3 J& Q, ~" z6 X } ( n: w" U8 i; U' M, i--------------------- . J( s+ f7 x* m, q2 g0 j7 W构造方法* \* N& o8 r7 Z& ~9 w
public AtomicInteger() { $ K7 X7 B' p/ J/ o! d }9 r; K0 n1 i* t# M6 ]$ i" I' \
5 v* `5 O D2 Z) Epublic AtomicInteger(int initialValue) {! u9 v/ C2 Z0 h( _
value = initialValue;, F7 I; m2 K( |, _
} , k" z2 h! ~7 Q+ I( B+ f( I- q1 O1 S9 N- v) L, i0 o
对象方法: 3 x5 b! W& e. ~4 N# d% ^ public final int get() { 4 m6 l! i+ V7 {& ^ return value;4 W4 W) n! E6 `6 m
} * c! v! G6 k* I5 C/ U% E u3 |! i3 Y$ |% i7 ?; ]4 T& |
public final void set(int newValue) { / p5 {& E5 H; }, A& ` value = newValue; . \) v% V5 r( V% l4 C% K) t& l }7 p: D2 ]" b, G1 K
. l8 z9 `7 F+ ]5 N3 x0 i% u * p$ {) p6 M1 \" Spublic final void lazySet(int newValue) {1 G% u, \! \6 B3 P% G
unsafe.putOrderedInt(this, valueOffset, newValue);! H" q! i/ Q2 o! \% \# A
} $ b8 i7 m! w. h* _) g/ p' U+ a6 v0 J; ~: ] [
9 D7 C; f$ Z+ s8 ]! d3 A5 ~' O' @public final int getAndSet(int newValue) {1 }9 U' T) I. L2 [! q
for (;;) { ) b! |- {. G4 K1 f# ^ J int current = get(); 6 P0 H* W6 n& a) p. E# u1 p if (compareAndSet(current, newValue))4 ?7 j0 g& A {! Z- L: n! r8 i, y
return current;& d! w }( S; b1 S
} / E9 d% W" o# s; t0 b# u } - K, Y) j' {' F4 j; O* {1 n5 |* l
/ \7 I; x, a2 K7 K/ Z, a, C; z
public final boolean compareAndSet(int expect, int update) { 1 p! S0 R. D) @ @$ r& q return unsafe.compareAndSwapInt(this, valueOffset, expect, update);6 D# {( J: c* j- L7 c
} " y$ q# |/ j/ W! Z6 u: `' Z- Z) R
& m0 Q! \0 U; |, a
public final boolean weakCompareAndSet(int expect, int update) { ! {# |! K* \* K% {7 p return unsafe.compareAndSwapInt(this, valueOffset, expect, update); + P9 {! m: j, N! q9 Q% l; p } : P% A0 G7 m* Y% ] ( g7 Z2 J1 x4 Gpublic final int getAndIncrement() { * z5 D9 F6 e) n0 ^& ^, _ for (;;) { & Q k. ^# _$ B! y0 e( k" h7 D int current = get();8 Q' G' a* b% {5 e' @) F
int next = current + 1;8 O, m0 h8 T0 ]" |. J. N8 w
if (compareAndSet(current, next)) & L) s; Y6 |. h. t/ E P3 } return current; z. ^( a, s# q p; U
}4 y0 X8 x3 Z; U2 m% v
}) X: I6 K/ y( x
, u8 w2 x3 S. U; B$ ^/ h W2 T
public final int getAndDecrement() { ; A c" C* Y- o2 f for (;;) {8 A) ~1 Q: I4 i2 A) \
int current = get(); 8 R' f3 N2 r2 s" N int next = current - 1; % |* k$ N: L9 L- Q0 ^, h* { if (compareAndSet(current, next)): |" Q1 Z% V4 ~2 D' V. K) q, ~
return current; ( ^! R4 O0 J6 R& D: o } 7 q5 v6 J& c' a }0 ^* {0 j4 Q& ~# U4 Q
4 j2 Y8 d) n% I" s7 T% j$ y9 r- ]( d; E4 L4 X% V6 w- g) Y
public final int getAndAdd(int delta) { 4 L! n3 w [: f+ g for (;;) { v5 K8 ?3 e. F8 d H' x9 Y. i int current = get();1 |# F" W/ \$ S7 h) @1 A
int next = current + delta;0 M0 ^% n. L1 {+ n: W1 D! A. W
if (compareAndSet(current, next))0 b5 V! W% g* l7 T1 E
return current;" J4 \8 k A( w5 G$ r* k
} " U! X( f, ?6 X% X2 Q ~ }, u: a% s3 c7 C1 c1 D" H$ n
' {2 ~/ q8 q3 D& \; A& V1 G2 l! E2 a5 Y
public final int incrementAndGet() {. [- U3 D4 N- v \
for (;;) {6 g9 w/ r# Z7 } Q1 s, H0 B
int current = get();) M: O7 B6 S& Y% j" C( a
int next = current + 1; 1 J0 O$ q- L+ ?0 t o$ L& Y if (compareAndSet(current, next)): J: Z4 O5 Y* A6 e. k2 S
return next; / r" j7 {+ P( X& J$ \8 E }4 B5 b9 U& X( S2 H) L0 f
}( y4 d# n0 O/ Y& _/ g& C
1 r, c+ R' y1 f/ X0 F7 _! r" n% t6 @# v
public final int decrementAndGet() { * `1 N- I( Q3 i4 a for (;;) { 6 Z* ^9 V( u0 n O int current = get();/ j& ?) B4 y, p* K/ ~. x" ]
int next = current - 1;7 {8 c' i! ~+ B4 I
if (compareAndSet(current, next)) ; \* U7 Z4 G2 B+ ^ return next;* V7 h# t/ s1 B% L1 v
}5 e# f# H0 T) S t% e8 Y. o; K* S& P
} : X- q' p- l& S" ~* a: Y9 S& ?7 p7 }+ j5 L
. M! K/ q0 j2 T- p5 C' @ ^, mpublic final int addAndGet(int delta) {" R: Q( ]+ Z6 i, i/ C
for (;;) {( y, @0 {) f. x& ~
int current = get(); 1 G0 ^8 l2 ?, k* d, W: R5 J$ u1 Y int next = current + delta; $ e! X4 o' i* j$ H3 ?8 c2 b6 q; \ if (compareAndSet(current, next)); m' ^/ K* R6 e' [$ f# _
return next;! x& `6 P$ M5 j) @$ y, v
} 0 V) X" P$ L8 _& W }6 [0 f+ ~$ [' p" h/ c' P
5 u! [/ C1 x# y" y. |1 b; g i) w& l/ l
public String toString() {9 Z* C' T- V& d6 g9 W) g
return Integer.toString(get());3 O _9 W# N, S1 b
} ! e* o6 Q7 Q4 x: l7 q ' F N: g8 u" w1 } 7 Y+ F2 W' L# M+ _public int intValue() {8 q$ u: ~" p& x" m
return get(); 9 G! F# Z, z, R. S& {% P7 { }5 Y& f' m# }$ f. `
: r8 Y. Z+ O+ D" p
. O7 B/ T6 b+ T5 N7 R- z
public long longValue() { " V c0 \& Z6 f& N/ w return (long)get(); 1 S% q: O6 v: H1 _ }8 O+ h& i, X; c) T
; W, O5 f* i5 ?9 q 6 U0 d0 _ R; j; w2 E9 F) Mpublic float floatValue() {* R) f; s5 T. [* B( |+ X1 d
return (float)get();6 l% s/ \+ E# D9 k
}3 M; b: }9 ]. R. Z
9 T, ^% `+ s8 |7 H! b6 w2 T$ b* b
/ E2 z5 O! V, K5 j2 d6 P) q
public double doubleValue() {4 p8 ]4 K4 V+ ?
return (double)get(); , u3 P) M- W4 g( H# Q/ h } $ ]" `; H! F: c. W& r% F--------------------- 8 t j, Q* j: l8 C作者:feyshine ! z9 N& k" B; m7 T: q- |: G. J
来源:CSDN : u: Y+ `- b2 w! o