AtomicInteger类 ! T7 k; F1 K; a$ {7 v' a$ l! l6 m. H9 G- D
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 8 X3 d( p& U' ^* }8 Y3 l S( Y 0 r; _/ H7 b3 o! S, o1 f9 c重点内容 , i7 r) w5 L: M* K- w+ Y& T ' P6 ]' |4 B3 X9 {属性:% h: W% ~) A8 ]" z; u
private static final Unsafe unsafe = Unsafe.getUnsafe();' q! M3 b8 C5 h3 w
- [- v3 b/ }+ D: M v; z* h. }private static final long valueOffset; ; g9 J; z* X# ~8 ]; r1 c |* K: @) p9 d- g
private volatile int value; # C! h: \$ i5 N0 U2 r- K+ x7 o8 f! G7 d2 H; m! q: W$ R: ]
静态代码块:4 o$ W( ?' O N( B/ c7 J- h0 ]' H& s7 w5 H
static { + Y; M6 Y4 ^( A try { & y0 W: S4 l8 D2 m j$ V, _9 V* f valueOffset = unsafe.objectFieldOffset1 a5 y4 m# ]! J& u9 M( {
(AtomicInteger.class.getDeclaredField("value"));$ D1 _' C! G. u5 e1 S
} catch (Exception ex) { throw new Error(ex); } / Y f' l1 @7 d }, @! B* v: N" P2 H
--------------------- . g+ P2 ^( P5 W, ^; [ o构造方法 ' I7 _2 t, o/ C& S' Tpublic AtomicInteger() { 5 Z; b) @2 `( I9 {# j }+ v/ D( v1 u( b: T4 ~$ _8 k, K5 |
/ X; x# R& A* P" J0 o: A& |
public AtomicInteger(int initialValue) {, t# ` W& N+ j; [0 \* j% Z7 q
value = initialValue;$ H; |5 s0 {. U
} 5 e2 y0 W7 l( S4 F; B: e# v; o0 q$ \8 \' o$ I! Y% H2 m. R
对象方法:5 i* B( d3 y# _9 l% Z8 k6 J Y7 {( V1 p5 G
public final int get() {* A/ K7 r) \4 v5 {4 i; l6 L
return value;3 ~2 W- D8 `) w5 a K$ a
} & f# a O( ?; }% R4 Z3 E% {) c' b) Z- v9 l5 I4 z3 v2 r* a, O
' I0 C l3 q/ X2 V2 O% opublic final void set(int newValue) { 3 ]6 G+ l: d7 ]) }: p5 k1 U value = newValue;& x7 ^( D7 ~- F/ o1 q M
}8 ~! S% ?0 K; p
% H T% z3 @+ v/ O
& G* y [8 U9 E* \0 Apublic final void lazySet(int newValue) {3 Z( ~/ b2 a# X
unsafe.putOrderedInt(this, valueOffset, newValue); ' N) F. q& Z+ u) D5 Z8 C5 r8 I9 s } . T" ?. {) Z1 e( a- } 1 F. Y: R' G4 L1 C, o1 }0 Q# b1 A0 y6 b9 }4 L
public final int getAndSet(int newValue) {1 D7 n3 e3 j2 R+ M0 s' x8 x0 Y
for (;;) { * d& I9 Y- R z3 z/ y' i7 l int current = get(); ( F6 Q% t6 B6 K if (compareAndSet(current, newValue)) 0 K5 L- O: D' J2 o% `0 M return current; 5 b1 H6 I/ \! \7 K0 B( \ } . `- W5 b# o" z* F- x! E! I/ Q }" Z( e5 j4 C# P; j
3 @$ a2 U: X: \% `7 J$ h9 X ) x" I7 [( A4 x3 Cpublic final boolean compareAndSet(int expect, int update) {9 Q. s I; A8 r9 r3 h
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);7 r- F8 d2 j+ g8 d% ^
} s8 _+ P. }2 N5 _ 7 U& R3 a$ e: L0 @' k" I8 H" L$ Y$ A/ G# j$ G: c
public final boolean weakCompareAndSet(int expect, int update) {3 G5 {* r' o1 q5 Z$ H9 ~0 E8 T
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); , p& Z2 f1 L) c( b, y) p& ~8 F& c } D; K% Y+ M" D0 P, ~- x& `/ V, p% {4 S; [% }
public final int getAndIncrement() { ( M! z+ d% T& x for (;;) {7 E( C& W8 a* v
int current = get();7 d) }, L) l+ \# q5 F
int next = current + 1; % Y! }( l; O) X if (compareAndSet(current, next)) % c% E' v( ]6 u return current;: q3 k6 n+ S- E ]( Z4 M
}* I8 y+ C, g y$ J3 y' E
}+ u( F7 b8 D3 p6 r) E
1 k6 l: Z4 I$ }8 }. m
public final int getAndDecrement() {8 w- k& d8 x2 ]
for (;;) { " V( V7 A$ ^! k3 ]& x int current = get();+ d0 s$ O/ r2 i. I6 X9 H) }
int next = current - 1;6 K% R' {/ b t6 L+ D+ D% q" T
if (compareAndSet(current, next)) 4 S# p+ f9 g9 o& ] return current; M, B* ^1 a+ v( { }1 T# n+ a1 b# F- p6 o+ T4 ?7 k1 U/ v
} - l0 X+ K7 q( Y. o& H! y 4 q+ H; @, Q# B9 w 9 u& d, y/ r# W+ xpublic final int getAndAdd(int delta) { 4 N( {, T% L6 W. j& ~ for (;;) {" q1 N: V0 {1 R) t( B5 E' L
int current = get(); , w! j, x t& A" _. U$ b8 p int next = current + delta; ; |; K" p% Z7 ?. p9 B if (compareAndSet(current, next))" O2 C" s0 K/ ~3 G9 V0 [5 K0 m
return current; , ^- {6 S, |7 f } p* B0 k; Q$ b: o% A0 K. h } % h0 T9 P- M4 C( e' X/ W1 t 9 m+ C5 ~9 ?9 E1 v2 C7 E( S8 H/ F' e5 ?& q& q% v: d
public final int incrementAndGet() { 8 W7 X/ r+ e" z$ p for (;;) {2 ^+ K! l' J! M
int current = get(); ! ?$ Y5 g! F5 W9 }" r% x' Q6 j int next = current + 1;9 O2 H D8 @! [
if (compareAndSet(current, next)) # c. L; a. q8 J; T6 t/ g return next;2 @1 f/ l% \: k; Y$ C) _( D" N
}: B/ [% F9 k+ k; n; q( f
} ' r j8 P9 C1 J. L7 H 8 f% D+ n, A d6 W2 |; b4 v 7 k' H! L1 x$ m9 k2 kpublic final int decrementAndGet() { 9 [7 L; a. q5 B0 N% L. \# H- u for (;;) {' I4 U9 e1 C5 t$ {* M
int current = get();! `' ~- ^- X, \( X& ^
int next = current - 1;6 w; G" X, I4 ~% L3 |
if (compareAndSet(current, next))" a% B e: j4 I' S
return next; # V7 W2 a5 R0 ~3 s7 A- Y }! n8 P l" q& ] v) H
}; z- N. N/ B6 Q
$ N9 D4 ?1 y/ ~7 Q6 L2 z
6 I7 r, E; }' c N
public final int addAndGet(int delta) {. u B5 H2 e2 O
for (;;) { * ?1 L& k5 L: u# H int current = get(); 2 k p' }% f- z, X int next = current + delta;+ w, C4 m: k. [, ?! T
if (compareAndSet(current, next))2 K, d! z8 K( S2 `. T& N# H
return next;3 U( P% Z& C" I' M: c" p
}1 f0 N3 X9 |0 r/ }. T/ h
} 5 z6 a2 N# w( \# }+ T& l7 X# M8 c2 \2 N2 K
; `0 h) v+ n5 K$ {: _% G, m6 upublic String toString() { 3 O. `9 i- G9 d8 i' H9 P8 ^; K+ B return Integer.toString(get()); / x% h' e: |. x$ v' C4 s, n- Q } 5 t3 V4 a0 ^& [7 O# Q; O/ Y : O5 F$ C6 \. q+ O- T: r: R 4 ]) g! k8 x4 l% Npublic int intValue() { ( p5 W$ _4 j, j return get(); 1 |( S8 Z. ~$ l } ( u$ g$ ^. j K: B7 t 5 k6 a- H, c/ I" j3 T1 [( {8 u) G5 _8 C6 v- G) l
public long longValue() {$ _7 L1 s5 x# D1 {7 u4 M
return (long)get();0 S3 l! N# P( J: x# u
}$ c# L: g4 h% ~2 [- O7 j7 ^4 F+ g
1 ]( e1 q/ e; C5 @* b9 k& ~" \2 o. F: q* c" Q) p
public float floatValue() { " s( v/ [& ^3 M) ?6 t return (float)get();+ a! r& q1 ?& O& C" n, b5 W
}2 f8 |4 ^& C; V
5 U+ S( ~7 i1 V- q) H, E: K2 X