/ m4 n# t* _3 D7 Z# v g2 b属性:' Y$ z# v: o' @) [: t1 K; d, o2 m
private static final Unsafe unsafe = Unsafe.getUnsafe(); 5 Y& w' b% v5 u, `1 c5 k ' t$ d. V6 x7 |( G5 X7 J M5 d5 i5 pprivate static final long valueOffset;1 s8 ^" S* G5 [8 `1 S: c( i) x( l
3 G: x E) l0 V( t" N4 e
private volatile int value; - p4 f; h) {. y2 T# c) Z6 x * m; @3 }; L X. \: r静态代码块: ; T. b" n! k7 Y; astatic {7 F$ g" g( E7 p4 k& q0 c( \
try {$ y; V$ h$ |0 w! K
valueOffset = unsafe.objectFieldOffset / j3 K: D. w- F+ k) e$ l (AtomicInteger.class.getDeclaredField("value")); 4 `' w( @) H K( \, T! Z) _ } catch (Exception ex) { throw new Error(ex); } / `% P0 z1 V0 \ }3 l5 P2 K$ q6 J0 b0 O1 V8 X
--------------------- * R5 n0 t5 |/ N8 w6 i% b4 ]$ B& b构造方法$ ?7 h2 R p4 v/ e
public AtomicInteger() {; {6 k4 @# ]5 N, ~
}! ?) e, X) H; F8 V* Z; k. W5 U1 H9 R/ S# n
z* d: W. S" `0 S
public AtomicInteger(int initialValue) {/ D0 ^ ~9 y; i ~4 r& J! w
value = initialValue;8 M! B5 K) B# S1 [+ _& C
}. J- q+ o+ g6 H4 n& I: n1 @5 V' G
: J1 f4 a; |# V% h
对象方法: , Z( o8 h4 f* ?* H8 I public final int get() { : Z. `, X( @; V4 }0 p* b7 ^& _ return value;- e% |3 C/ a, n% l
} , I0 r( |: M% }/ D6 H5 t! N' u) ~9 w
, S+ L Z0 L5 w9 i( A8 p) |" b7 N
public final void set(int newValue) { & N' c& i$ {. o* P5 J0 B value = newValue;5 ~, v5 d$ c, F) ?( ~7 Z8 q
}, G( z1 ?6 U$ H- R- B
% Q" a) {4 |+ r( } ^- i& T
: \8 \2 F/ b; p- y
public final void lazySet(int newValue) { ' x' p' S& v$ d% q unsafe.putOrderedInt(this, valueOffset, newValue); / X1 p* u1 B" u' Y. f. e } 1 V9 e8 \) l0 j' h, t e1 |! M( N& s/ O/ X
+ t3 c# x5 v, R/ @+ i0 Z9 G; U1 \public final int getAndSet(int newValue) { ' x; G" w* W1 f" h" ?# y for (;;) { 5 M7 I5 {; g, A* P int current = get();8 E, T1 {/ g( t( z$ ^! s
if (compareAndSet(current, newValue))4 y" V. K8 V* y9 B7 _: t4 B; O4 z
return current; . o) B$ q1 X" v( i: K }( m" v: U, f% I1 M! |3 f5 c# O
}2 J8 W, D. r: ^0 ?" V! g! X- m5 n/ K
- A/ t! W: w8 {9 O% z) d, b% O5 K8 j
1 w' v0 k* R* M0 h8 x n
public final boolean compareAndSet(int expect, int update) { + y' X6 t8 w) N h$ ? return unsafe.compareAndSwapInt(this, valueOffset, expect, update);0 Y, [1 t1 ?1 A- t; A' x; x
}5 z6 q" @) y( y8 G
8 k$ u% E2 n- ?& T- w# h
2 J- m# K* D7 `public final boolean weakCompareAndSet(int expect, int update) {7 ?1 l3 w% M- P- T3 |4 \
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); + b8 |9 Y7 m7 A } 6 _6 r; j) s- N" F4 T% e- A4 E; I% Y" b9 Y9 h @! C
public final int getAndIncrement() { 7 |, ]5 E( e N4 a" H3 r+ p: X) ` for (;;) { % K: a3 V0 @/ \, J( m- K int current = get();) Q# N" k' q$ Q/ h( B3 r7 g6 ~
int next = current + 1; 2 M3 r. L$ {3 H9 P( v+ `3 q3 ` if (compareAndSet(current, next)) ( K, s; B8 `" ^2 o return current; $ g' z; |2 s T }! G5 v {$ r! l% q5 Y" Y, M
} ' n8 t) e. W, K4 r8 \ $ r. c8 a! T u6 \8 O9 v- M7 i7 }public final int getAndDecrement() { 7 L, h& x0 |$ w) h3 ^" {* { for (;;) {8 @1 p6 v; c$ E. z1 P
int current = get(); O8 s2 a9 D% ]. u! H/ B; O7 h
int next = current - 1; " N$ |. ^+ ~! S/ N& ~& D- [ if (compareAndSet(current, next))3 f4 C8 S1 M: g R
return current; 4 J* l6 B; B# U$ D1 a }8 q7 s8 d$ s$ `! J8 r O" M! l" }
}# X8 l% a+ a3 ?+ W' l
+ z* |4 O5 q7 R* x0 @0 ] * F6 H8 o- _! g* e6 O E* p, Ipublic final int getAndAdd(int delta) { 4 l2 q& r0 r9 w. M7 f9 d% a0 X for (;;) { . N# g* E# ?( [/ ]2 T1 s int current = get(); , K, o4 q k+ N, e4 m# i int next = current + delta;% C. G% X j; z9 s: C
if (compareAndSet(current, next)) 2 ]# R; r1 V/ ?$ J; h6 M/ @* j return current;2 u. W$ F7 D6 X
} 0 n/ M; l$ b0 P+ ^7 y& W }2 g' ` E0 E, D3 u/ \
+ W+ {: n A6 F( R! [0 Y. A% o6 j: A# L1 u/ Q8 ]
public final int incrementAndGet() { & O1 S* c2 o! l" z for (;;) { 7 d$ _5 c8 c5 W+ h# ? int current = get(); [6 U! f9 [% L
int next = current + 1; - P4 F0 l4 y' Z; g/ Z7 J" Q if (compareAndSet(current, next)) ) ^3 X: Q. X; ~( _/ @9 v return next;- n2 _4 m. i/ U, n0 \. F) k8 u
} m1 A% j- {! P' U2 Z
}( ]) i7 w# T6 i, r& G6 d% l; o
1 J6 ?* Q' x9 S* ~, e9 ~2 I- ^" h. I& I( m @
public final int decrementAndGet() { 8 n' x' ^/ x) b6 S. {, k7 H for (;;) { v, J: i6 ]; L) |& s
int current = get(); % a. Z0 [0 x4 a5 ^ int next = current - 1; 5 Y7 l9 ?' ?! h% \( @* q+ [/ A if (compareAndSet(current, next))4 r. [, f D# A [0 Z' ^
return next;7 q& N4 I, E$ C/ s5 z, v
}: _/ {& t6 y" Y0 s3 U
}: f; r$ k' [. j' d/ X
+ x2 F$ z1 d( g) l( l- c 0 K, K- J: p! K5 q/ Hpublic final int addAndGet(int delta) {* ~/ ]% T- n! J. ?4 x5 f
for (;;) {: [8 q+ ]! |, q2 o7 o# g8 J* s9 m
int current = get();9 b8 |( h7 [, P5 C/ L. n: }
int next = current + delta; 0 |. Q) j0 J# _$ o/ G& Q if (compareAndSet(current, next))0 t1 n& s, l. p+ W' N- Z
return next;% J* S$ i2 L/ h O
}" W8 x5 W0 Q9 H3 N8 I8 b
}8 ]" \* X2 G# i
$ G, I" V- w4 y7 e! C
! y/ a! }5 X, b+ W( W% {
public String toString() {/ U7 |8 D& n1 [! u0 G6 N9 v
return Integer.toString(get());. J& v; v% Z+ r# V- D* q
}) u( a% \' w U4 O. K, I
2 F2 z# a" O/ k$ j" d: P2 F ( D1 Q. B& D" l* Fpublic int intValue() {1 j( P9 b! ^$ v' J' }) P
return get();1 K6 O$ J! b+ i" r0 T) I6 M8 q9 K# e
} 0 e# k# u" L, Q) z- W. g3 h) `
, u4 Z4 z) _! Y: M- Q1 C, N
public long longValue() { 8 k8 b: ~; R% n: s+ Z# X( i return (long)get();: n/ l' Y9 ~. E% b5 l' b' e
} ! u( g6 B% g( P 2 I9 w* A2 y, g+ |# I6 K0 @- ~" \, H" d9 I( ^9 i
public float floatValue() { 3 Q6 a3 E5 g N* b' u5 Y! w return (float)get(); - B p% X6 {0 V6 V } * M1 k2 i# W% N. ~! J+ s' w( s; ~ . u8 T1 H) E9 j" K- L$ a) t& Z4 j: B+ G$ z1 B- n* a+ J
public double doubleValue() { 7 V, a& b0 C) s/ u- E return (double)get();# p# S" M2 Q; ?* R6 w
} 2 Z& N, R" L4 _" S--------------------- - R7 m# I/ N" D
作者:feyshine # Z# N4 j- t/ {' V, l6 j: B
来源:CSDN 8 H& m5 t" j `, t3 L ! t- f0 m- X: J! g% |+ }5 _+ L/ \$ }0 c6 |9 s% G( h% r V
! t+ L# P, k" r
8 X6 g5 S- x2 R' Y$ `5 x$ Q2 B s & t+ K' N$ A8 p% M