; b! J; W8 S2 ~8 w8 ~属性:+ O- h2 `; m' l {& v
private static final Unsafe unsafe = Unsafe.getUnsafe(); + u4 ~2 H6 B: Z$ D7 L3 t$ x # K2 D2 U0 H& _3 E1 y4 \private static final long valueOffset; 7 V8 T! i G& M* n" P$ s; q - U) ` e3 O+ k5 k0 Sprivate volatile int value; ! f$ P7 m$ ?! n$ i 7 W2 n8 b6 L8 \ p$ @) ^静态代码块: 9 K) ~2 u* t# [- }! j0 ~2 Fstatic { I" \, |6 d6 K5 L) j9 R+ I* c0 n try {) s+ o; P% R0 o% ]; E! _
valueOffset = unsafe.objectFieldOffset0 c: @; v% a* h+ s7 v( i& I7 D3 D
(AtomicInteger.class.getDeclaredField("value"));# ]* I, W: s, a
} catch (Exception ex) { throw new Error(ex); }6 p* t% S0 Y$ e* }
}1 \7 \2 E d2 _1 i
--------------------- ( a9 L7 O) c3 ~! V$ n- d, _* n. C
构造方法 0 C0 }+ ~6 B* X: O5 [# w# J7 Hpublic AtomicInteger() {0 [8 W/ M8 _& O3 T& o7 I
}) b% h% o# A, H# x0 Y& V {6 @
8 y% V4 ^# ^" I* T/ H/ B; epublic AtomicInteger(int initialValue) { . C- Z" f( U& [' g1 N/ n value = initialValue;0 I0 M2 Z5 ]$ l; Z' k2 v
}% X( I4 a3 z7 V- G8 f9 o9 b2 i, a) [, Y
0 U2 p+ D% k- F
对象方法:0 V; ]/ E) Y, p
public final int get() {* b# \! g) S! r4 r$ N, ?/ `
return value;) W; J9 ` d# q0 v3 _/ V9 e; A
}! x) b: a( O$ B- f% i; S
2 {9 x: e: x% S; ] L3 a* }+ r- P8 I6 _4 }* @( s
public final void set(int newValue) {) Z% r: D) w& N& D0 l L
value = newValue;8 B0 }7 x7 W2 u. n6 s* k8 C( I% t# E
} + k5 }. \; q8 K3 L, d 0 R0 m8 b4 _1 ~$ v u1 x - L7 S8 j. ]' l0 i; o$ \6 [public final void lazySet(int newValue) {$ _) n+ a, [) r) `7 B, x
unsafe.putOrderedInt(this, valueOffset, newValue);9 \& W' m5 h& R& s, c8 L
} . u5 u. d2 V3 N9 @4 L# J2 s1 A2 P: p; i8 d' O
# B: P! Q3 ?% H$ d0 U: k$ i6 [public final int getAndSet(int newValue) { 5 K9 @, b: w" ]( i# |6 R& x for (;;) { # c# M& F. |: O# r3 \ int current = get(); 3 ]6 }. T* t6 G# F if (compareAndSet(current, newValue)) 2 }/ Z+ a$ K8 Y7 G+ {+ R return current;. \4 z0 K2 |; ?/ b9 @; ~
} 1 P e3 ?( h n } 7 g8 F& T8 h" O8 _- A' i# R . X$ Z: T1 e2 M; y ) Z/ ?9 m/ z5 d' Tpublic final boolean compareAndSet(int expect, int update) {& |' y6 [3 |; Z2 |$ Q6 s& J
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);& v' s6 @1 k* l2 G
}* u! z J+ r. G" U6 D7 M
5 {, t; u2 K8 [- k6 E. n5 K6 w5 @1 ]
public final boolean weakCompareAndSet(int expect, int update) {- k. ]8 ?% b8 X% ?/ N
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); + G1 }) A! p: M+ P } . R9 y/ f- m1 S4 V. X4 ^6 E1 Z. P0 j+ Z5 d
public final int getAndIncrement() {9 n9 [) m0 v) J
for (;;) { 2 Y1 l8 [$ N2 `! X. E" `* G int current = get(); & U4 ~& Y8 [' z+ L int next = current + 1; 4 F7 L9 j$ F7 q2 j if (compareAndSet(current, next)) ( P2 x. f$ s/ R; {. L return current; & X- Q2 ^7 P* i: ]8 r/ L } ! z7 ~: x, A4 l7 v8 l& p* g- s* S } : |( \" a9 i" f. E) k0 s' w! Y# D' |+ K+ C9 D( S; V# ^! a8 e" o3 Y
public final int getAndDecrement() { C4 H* \% i& o. y* x* u5 E$ R
for (;;) {' o* p- k, R* k% l; m% X0 b- m
int current = get(); 1 ^, [9 {, c5 N! {! c! F: u+ h int next = current - 1;. |9 l( ^8 r: J0 [$ [0 l
if (compareAndSet(current, next))& y- i1 p& W+ z: } {7 [4 c
return current; " i3 ]9 {8 D1 G, Q' U, Q% ^ }+ L: p3 o; ^2 G+ u- C
} 0 d2 a. W5 ?$ o! V& m4 @* M9 y) {1 s
, B; N# t: z# ^ j, C* b f6 ~: X
public final int getAndAdd(int delta) {8 f0 T- _% W7 ~3 Z+ Y, B' r: `
for (;;) {$ M$ [: j+ l) s+ g4 u6 O" {1 v
int current = get();6 i" E, {6 s9 C; U
int next = current + delta; ' e4 q7 z+ R8 F0 d if (compareAndSet(current, next))5 O* j$ ~$ |% U
return current;- y& \8 ^ k# s" D! k" b6 B
}( |2 D# P1 o4 ?9 g6 ]$ s. e
}" J3 L0 E- J* j( g# z0 u
, I( C5 U- g% D$ _( Z # O' L/ d N0 _1 z1 Ipublic final int incrementAndGet() {* ] o/ _3 W3 t5 M; b5 i# J: C" h) E
for (;;) {# y/ D" R# o' G7 Y, b" @
int current = get();9 A1 k5 q! _% ~4 y- s. ?
int next = current + 1; , A+ O4 {+ O, E if (compareAndSet(current, next))* }% a3 L, v% j- B
return next;' G9 i' T) h4 k6 U$ g" L+ [$ [( \
}8 t' q0 i4 {4 e. h: H9 d
}( n# l4 f4 b% r! D) R; I- w
( {! E9 k: W" f0 R * W0 D$ W/ |2 T, _% v! Wpublic final int decrementAndGet() { & i5 R" Q1 v0 F: I for (;;) {! T' j' _( t7 H- T7 t: s
int current = get();- K! ^& ?. D) k! @
int next = current - 1;7 b _9 q5 ~1 W" m( m. G- N
if (compareAndSet(current, next)) 1 b- p9 [3 b }: f return next; 5 s' l* g/ i( _9 d! a. f }1 Z; D, F1 @; w1 K
} . z4 p2 \8 ]7 g# K: y K& a; I. \% b2 E9 h- Z3 c: Q; @
. ?( O% U y" ?( D
public final int addAndGet(int delta) { ! K; e% ]+ u! v& C4 L) p for (;;) {1 u+ C7 ]4 `& B! V& g! V8 n! d
int current = get();' ~5 O5 r G# H$ [+ L4 s
int next = current + delta; & y6 F! b0 }8 h- J& S9 O( U if (compareAndSet(current, next))+ z9 `4 s) Y2 [2 j/ A
return next; U- J: F$ K$ o$ [$ s$ k } . p7 H6 q# x: D: k0 n } : r) h. P$ C# Y+ d, Z& t- L/ U5 A, a2 m1 ~0 G
7 C5 o2 W2 q, _% Xpublic String toString() {% q) Y, a, y5 e, W( _2 ?( o
return Integer.toString(get()); 4 S s1 H0 Y$ e } " N/ F: g' W/ }6 j- `! U, J8 c; P( S% n1 [: }& |' j9 i9 ~
+ y7 K+ _2 f, O* x- F2 B( y
public int intValue() {; [3 ~' d3 r: {# s$ v
return get(); ! F0 l; \: C# C( o8 ^ } 5 K1 m- G& N" W3 G i& }4 _0 X+ b* R4 |6 _% c* ^
R2 ^/ w- {3 V d q) n1 C; mpublic long longValue() {; B6 `) i+ r" M1 |3 [7 q
return (long)get();5 o& c. m; d- E: m$ u
}+ G+ F! v. Q' ^/ K1 v
) f" T, f' P" G% f4 D, `
1 K1 D2 ]! x- k' i' rpublic float floatValue() { * d$ ~8 D; H6 x! f% E3 m return (float)get();0 K7 v2 p2 X# ~$ J4 ~
}6 D2 c. i; y& f' ^) |6 d
! Y% y, k }8 T/ H# g3 O, P# c ( a: t5 i0 h0 `# tpublic double doubleValue() { ; ~+ Z, ~, A. J2 w1 O, V [ return (double)get(); # \7 J( O. c2 }6 i( C& m* K } 4 L$ W l9 M; @/ f, _" B--------------------- : Q- _* K" c' [( U$ ^
作者:feyshine ) w) l# @0 [; s$ u3 J' V4 z* R* F
来源:CSDN & E) x: ]$ F# \* ] 5 T& m' S3 e }+ }5 u& }: I+ P# I2 a" J4 s6 e; m2 R8 A
* x% R; R' z; o* M- S
" g; p6 N; F; S