* g; A3 p& y( z J$ q% K这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 $ Q: W& C$ S5 C5 [! G) I . I9 Y9 x! I( e$ c* I重点内容 4 v. U0 a" q* j. l, n5 ^% [ 1 T6 f6 B4 s5 l4 \2 S属性:' d" b B) H: q5 V& B2 E
private static final Unsafe unsafe = Unsafe.getUnsafe(); ) h4 e6 A# a5 _ W! R+ s: @* q3 L& U8 K3 Yprivate static final long valueOffset; * C3 w; H2 w: I$ d& L+ Y9 H, R x5 T$ @: r
private volatile int value;5 h7 }: C; |% o( w, T5 F
0 w5 `2 P* M. g/ L: W( u$ q7 l( G
静态代码块: 3 c$ `( p) a% p' ^static { , F# o) J0 ~6 F: y try {/ X8 s0 B3 W% A% N7 I& S( `
valueOffset = unsafe.objectFieldOffset - `7 l- V# l. V2 q0 Y+ k (AtomicInteger.class.getDeclaredField("value")); . p4 T; V J1 ` } catch (Exception ex) { throw new Error(ex); } - X. ^" W' }* n }6 h$ q P ^ z7 C9 [
--------------------- , G( W/ ~* o3 _6 H" f$ K+ ~/ |
构造方法 ( G" u3 ?: C2 c+ Opublic AtomicInteger() {/ U7 J7 n) i3 A& @0 l3 |
}7 ]1 g6 W' M5 e
9 L) p6 N* ^2 T B8 z3 S" P6 Gpublic AtomicInteger(int initialValue) {' j: x) f; ]4 }! d# ~3 J. E
value = initialValue; 7 E) V; W+ Q. i$ b } 6 E- Y; w3 `# d( |9 V/ [ P) `! j% q# d0 r+ q7 F9 H
对象方法:' e/ n* V6 K6 _- Z
public final int get() { 7 W2 h! f* |" K% D' P% d return value; " _: Q: z% z! O! ?/ X$ ]% A. V } O S( S7 p* ], S# I# D3 p6 r7 `) E( g/ ^1 y
' b2 m( _7 t$ Y) K
public final void set(int newValue) { , g& g! ^0 |8 Z0 c value = newValue;: v# `& c2 m V/ r3 S" L) k' B1 n/ g
}' e7 t/ i1 m& t
! t! e1 E% G6 G. ~1 R# X + C! f+ u9 e R; J* e( y* Npublic final void lazySet(int newValue) {9 @! f% K8 y) V
unsafe.putOrderedInt(this, valueOffset, newValue); , h5 `6 @6 _7 @+ x! t } & I6 D3 Z# w* D6 ] $ U7 z2 R L! j, G" Z6 W3 p 9 N* a8 H: K! C. @9 Tpublic final int getAndSet(int newValue) {4 @/ v9 `- i$ m9 L, b6 l0 Q
for (;;) {0 `5 l: ^8 r k
int current = get(); & J8 F+ c* E; u' ^ if (compareAndSet(current, newValue))# S; J ?- t( v* R. j( J0 Q
return current; U8 E9 B1 z, i% S3 X }' l6 x, v- Z! G5 I6 F0 u& d
} ) p& m2 J; x6 h4 L7 M# F# h, v2 S2 [
* U. K2 g. t( D+ O
public final boolean compareAndSet(int expect, int update) { \3 I0 x" g2 k( I5 Y) {6 j" F
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);& U& N% ?/ ]5 } S5 Y7 d& O
}, o1 o' C# Z- c
* k* A! d+ F6 P$ X2 B% G6 }
" |1 f% N4 f. j
public final boolean weakCompareAndSet(int expect, int update) { , E5 ^/ N% R3 W2 M. N& G" U" G0 z return unsafe.compareAndSwapInt(this, valueOffset, expect, update);& P! X) z/ ?2 z# Z
}8 h( P/ A7 O1 ?/ F. K% V
" I' ^, f' ~9 L9 b5 A; r
public final int getAndIncrement() { 9 P) l" f' y1 ]' A- F for (;;) { 2 D1 r- a2 u$ s! }( ^8 j int current = get();# E, u/ \+ b$ U# `) U7 r7 M1 e a
int next = current + 1; _' ~, @2 F2 x if (compareAndSet(current, next)) ; k- x! U0 K; q% }( Y5 P( f, { return current;" j5 \+ v" h b1 ~; C- I
}& i2 H& z: t% q: J: e
}4 o8 w4 ~( ~2 l7 V% g* d
( w% S3 Z' D+ } n( V
public final int getAndDecrement() { 7 [$ J- J1 o, ^3 s' m4 m for (;;) {. A, B& g& r! r' P$ j i
int current = get(); # C% Y# j6 H# x( t int next = current - 1; " K6 W. u6 d- z* S9 f. X* R if (compareAndSet(current, next)) [0 ^9 @# }! w2 f. C4 w0 V9 x return current; * V. Y# z" x2 z } 9 t0 _+ A7 W% A9 \. a } ! Q7 R7 j8 H. h5 `7 F- ~% d( Z1 D F
1 R1 T" c! x Q; O4 Q( e$ U: spublic final int getAndAdd(int delta) { k& Z. Y5 N4 H0 i: @% |4 T# X for (;;) {/ W F6 Z# a/ Y# h C) \( B
int current = get(); & g; q6 I% b b& e7 C( s7 M; f int next = current + delta; . o5 X! H2 Y1 D6 B/ P if (compareAndSet(current, next))# ^- u1 e0 l) @
return current; 2 R& v6 N1 Y% _9 V ~3 c1 x }: B3 Y; ]2 C! L. m/ V* t
}3 v @& ~$ }9 v' ?8 p
% G/ k- I. g. q7 v' s, B4 W" O/ U' `
+ R3 N! D! g& I+ Y/ }
public final int incrementAndGet() {: b. Q( m5 w# h3 x) E, ^" G6 H3 c
for (;;) { : [' L; e6 B8 i" ^' o. D1 z2 l int current = get();" Y' ~0 F! {' M( x3 e" C
int next = current + 1;- D/ \5 j5 @% ~4 X6 T. J2 G F" O
if (compareAndSet(current, next))5 U( U' R" t e% G. _
return next;9 r( O+ _" b+ E9 m2 x
} 4 B: V8 o. E/ ^ }0 R2 Y x) ^9 q7 H. i$ t, r$ v: K( k
/ p) h, K, s% k: }/ G* t) o6 e+ j+ x# f$ O6 z
public final int decrementAndGet() { & ~$ U8 T3 z1 C" L! c6 z for (;;) {$ Q* k" H* A% O2 }# g. R' Q! @
int current = get();8 u" Y, |5 D# u
int next = current - 1;+ h$ m" \4 {; r8 a* E0 t, }
if (compareAndSet(current, next)) 7 `% |9 F( U# _5 l+ K return next;3 }& G, M5 U& E8 ^
} - T% s2 a7 G2 y1 x2 q; R2 [ }4 X/ _' W1 h7 b
' N% l" L! E9 o& m9 c& o: g5 D
" g. d+ z7 ^7 ?/ m7 {
public final int addAndGet(int delta) {! D2 e4 }+ o- w. a
for (;;) { ! ] }1 I! J m, x3 n int current = get(); & Y0 @9 O! [; _; r) C' k3 i int next = current + delta; % Q) T- `( J2 {% k( w if (compareAndSet(current, next))4 H$ p' G* G2 t5 e9 n
return next;- R7 d2 a3 G" b* o: u
} 1 r3 }) T% f: { H8 o+ w }, D6 U) d2 A, W J5 ~( R8 j% G, k
. N, G+ ]+ T1 v2 t9 G ! X3 l9 Z9 t8 V9 L3 t8 lpublic String toString() {! R0 ^9 C& v a, Q
return Integer.toString(get());, F! y: k% B' ^' z) n# O; n) O4 X# N
} * g4 q0 f9 m" C$ j ^$ {4 L% x # H7 T5 K3 Q$ u- u% m 7 K. s0 T$ T" w+ x" ppublic int intValue() {4 \4 W" \# E3 K9 V( J
return get();" f. ~2 g/ H6 J T
}7 |) W w4 u% b6 m7 F5 m
0 H0 D7 d) S$ ` * _* w, I% E. c' ~public long longValue() { 9 I& I, t, J+ ^, I( u4 ` return (long)get();8 F, {& \. i4 i% X' F
}) s$ Q3 f1 j, o
- e9 S1 K" X! E2 s' R * k, l6 T4 l5 G7 P* s9 S5 R3 X& ppublic float floatValue() { ( h8 `; b0 {) I* y. M; @ return (float)get();. {# g( a* s4 i" D
}; @& I6 ? x- t% r. T+ J
5 ]" ~+ ?6 r/ X) x* g! W
6 f1 ]+ X" S% y: p; E$ ypublic double doubleValue() { 3 D" `6 H4 y4 X! A3 q return (double)get();0 Y- Q- s; n, p) g
}6 l: r- i3 L/ {3 z
--------------------- 6 m: u" m6 {9 b8 u5 O/ g Z+ T作者:feyshine ) ?$ ~# ? s4 u# P来源:CSDN . N( Y4 Y4 e3 q3 |$ s# k0 Z
* N* s. d% b# g4 c0 o
$ g) I6 K; V) j* m$ u5 _0 X0 A+ e; a
* g) v1 u4 _) T. n) R I