9 i: V: o: t' H! K( h q% _ \静态代码块: ) B! b; f6 ~# ]5 ]static {$ O/ u' c- ?% a* B& L
try { k. y; ]9 |+ C% _" |) N
valueOffset = unsafe.objectFieldOffset 5 Y: r: b8 y" E8 n& }# j1 d" x: m (AtomicInteger.class.getDeclaredField("value")); & y- f# h+ Q$ I$ p8 S3 p% y8 ~- n } catch (Exception ex) { throw new Error(ex); }, [% w5 S) O& B" t% ?) J* j% g
} 9 r3 {6 R' ~3 I--------------------- 0 I8 o+ t3 [! k. S8 d) O构造方法& a6 z& A, m/ X( P ^( Y
public AtomicInteger() {( z0 f N$ `4 {5 h6 `' @& w
} - d( S' k. W' u2 H4 y % Q# b1 f: ]- O) A, Y- `, Qpublic AtomicInteger(int initialValue) {( G8 Y6 P- O$ N% D, @' L
value = initialValue; : V9 z% \ ?) _, M/ @0 R* ` } 1 Q8 ? K- E" [( p0 M3 | r8 q: J0 H }0 `6 A
对象方法: 0 F& {1 S) ~8 a. X8 h' w5 @; r public final int get() {# T" ]8 x$ \& r& @" W7 M) G6 u2 K
return value;1 U; H; q2 F9 ^( ?
} 7 |; G: y* `; V8 a# Z7 a' l, X @# g & u. f, \/ V0 M' p3 S) J4 W 1 y" A& ^, `; s2 u, R& f1 Y& [public final void set(int newValue) {( M6 I6 {: v7 B6 K* |* G* s
value = newValue;- p! i. |6 h% o
} @* r6 W) A/ T
W. {, u2 w$ B9 @# K
5 ?1 M1 O) k, n; q3 Apublic final void lazySet(int newValue) {$ U3 j; b6 p" F. Q' ?! Z
unsafe.putOrderedInt(this, valueOffset, newValue); 9 C$ g( {6 E5 j- _$ X }; q, u; k/ E2 E. }1 }+ J
9 T3 O" E; X) u0 ^5 m% M ) p4 l' _2 j( \2 lpublic final int getAndSet(int newValue) { - R' I6 m9 G' g4 q1 b* y for (;;) { . S* @9 |: T' ?& P int current = get();( A, H' x* o! h2 w/ e- o
if (compareAndSet(current, newValue))5 y ~ z# Z. U" ~
return current; . q& d$ `% j5 f3 V# Y: l }4 A, f& l8 ^, X/ X0 `
} ' J0 d9 B- H- @1 p6 h% i8 P- s" z( s+ F3 o& Q
! J3 m" P2 s+ y5 @# X
public final boolean compareAndSet(int expect, int update) { + P1 A/ G: R$ c4 t return unsafe.compareAndSwapInt(this, valueOffset, expect, update);4 j H- Z% Y/ s
}/ D3 }/ |$ R3 F8 e4 u; @0 V
3 u1 }9 S5 }3 k$ t
7 v% U& M- ?7 r9 zpublic final boolean weakCompareAndSet(int expect, int update) { ) H/ @! B. J# M6 r: ^4 q return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 1 G% U! m- [, z q& n } ) r2 Z* i" L! ~4 Q$ B( k2 u& R0 u. ` z- G
public final int getAndIncrement() { 0 ^8 C- _6 Z# _. ? for (;;) { P; J m1 Q9 ?( y int current = get();( a4 E! [2 ?' ~# V( ^7 I
int next = current + 1; ( H1 e$ T6 u B) u0 y- { if (compareAndSet(current, next)) 0 X" C; ^8 _5 c3 c* n2 e! x9 r return current;8 H' m2 X2 z) d- h
}) x* l# p) s) _* {
} 4 |+ P! S/ a) v2 l. m! x, \# ]9 U# g2 j
public final int getAndDecrement() { & B6 C3 N7 N( T( @; @/ J x# ? for (;;) { $ w. ^( k& a) s( ^6 L- x int current = get(); 7 G1 K1 H) n9 d" o( n8 y int next = current - 1;7 X/ \$ T- _2 v& @$ _
if (compareAndSet(current, next)) * O8 L5 K8 q6 j$ R return current;+ G l) m8 x4 v
} 9 M+ h7 e+ l2 K5 y# u } 6 \0 O+ m' \6 |9 l2 [. d $ D; \: P. ?# H4 x. T% m 0 [; i% _6 v: {& k G, spublic final int getAndAdd(int delta) {" h: o& c/ S9 w8 r: R. _
for (;;) {; k; B4 [! S% p/ y) a9 @
int current = get(); " a7 ]3 V. r4 V$ ` int next = current + delta; 5 h3 H( M$ j& M( Y) s# { if (compareAndSet(current, next))! X' K. W: g( Z4 h- q& V' J
return current;6 l- O8 n+ z$ {0 n
} 7 O: k2 L0 e1 x: ]* O* Q+ c! t \ } : E& w+ I- M1 T5 |% h. p. D9 G6 {9 y3 b0 R: s4 e
( P: u* U! |1 J6 y2 ]public final int incrementAndGet() {) m+ U" M. }, e2 Z" B0 R3 q7 ~
for (;;) {# T4 @) F0 {4 L& `, P
int current = get();8 L2 E1 D% G1 t
int next = current + 1;. J6 Y3 c* M) W
if (compareAndSet(current, next)): k& j9 Z C- q" F' C4 p& T+ M
return next; . x( D9 Z% x3 |. R {! y+ P7 J }$ w" X7 c1 f- n; x0 n( p& |7 w
} ! M; a0 _' k6 }4 @: [5 r ( m0 x2 m: e, @9 y r8 b0 U7 L' ]$ W& r9 t' ~/ O9 l( b' N
public final int decrementAndGet() {( q* h9 V* V+ p2 l# r9 y2 a
for (;;) {" p( Q6 f6 p$ A
int current = get(); ; G! @6 y, q5 {- g5 `" A" D int next = current - 1; ; Y1 b( f- f9 t6 g* z/ U if (compareAndSet(current, next))% ]* i! I# `$ s8 m3 y+ I+ S c5 {
return next;* i* d- }. r1 L' @" {! y
} - E2 z) {! v4 C0 O3 ]& ~. u } " q8 S L, F2 R0 h0 a2 O+ @, K * C# G4 }( `6 ]3 t 5 D O# }9 N8 w7 J6 z- x6 c* Bpublic final int addAndGet(int delta) { 0 O8 s; D' e; R8 G2 ~0 f for (;;) {4 p6 X+ d/ p' e, _- g( b
int current = get(); 2 H. v: Q; x( b int next = current + delta;8 v9 }% S |8 j9 s) r" x
if (compareAndSet(current, next)) $ K# U5 h. l$ M( R9 O3 n' u% M) Q2 D8 p* x return next; ) |5 v: P# @" |8 Z/ G" S! \# P# P } 7 u+ z7 |' \ V: ~ }9 p4 d9 ^0 p6 O; `
; Q m+ ?; c) V' @) } ! h2 ?8 g d$ dpublic String toString() { ) y( ]0 s9 Z3 ]: l( x. O Z return Integer.toString(get()); n% L, l- Q7 X4 U
}3 ?5 y1 L$ J3 R- U
7 g0 U% s' `9 Z
C6 V8 D% c5 E% l; t# u
public int intValue() { , j3 a, C* D+ U5 c0 [* } return get();+ u8 u# g7 q% D7 X# g) b$ r, J% f6 D
} * u1 i* _/ e" {2 J' V# _* W$ k, ]3 V" L" m( n, P3 r
+ G; N5 W! p) t$ bpublic long longValue() {3 m9 p6 |# a4 D7 A+ U2 D2 m+ S$ Y
return (long)get(); ; x- {* l/ E9 b } " a; L' Z4 m- J4 a& l- e3 { 9 ~9 p; c6 Z1 K. n6 f$ K$ f: ?# z* x
public float floatValue() {; ?: ]8 V3 h+ v3 e& {
return (float)get(); 2 n% N% c: c6 w }6 Q" Q# Q2 W# C, C; l/ f4 x
" b- |1 T( r y
- f$ W' E0 T7 a, F& m4 u: X! |
public double doubleValue() {4 e2 y4 @# ?% A3 T/ ?" ]
return (double)get();1 g& _) T! Y1 r4 D5 u
}( ]6 y0 i# z/ Y' r
--------------------- , d' n6 E" l3 f4 j+ D# T# d
作者:feyshine % V6 ]! z" M1 H" B5 G来源:CSDN 4 {! [8 p) l' P5 f8 r: f' Z& f/ x3 i9 j2 S ]
. P+ P* j0 p P- G e" N5 n3 h" I' U- y4 ?6 d
6 d9 o* y3 N6 N, p