标题: AtomicInteger类 [打印本页] 作者: 杨利霞 时间: 2019-3-23 16:04 标题: AtomicInteger类 AtomicInteger类 " q1 w* {+ ~2 E+ ~ 5 i; h/ @5 f( Q i5 \6 k这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 * w$ R( s) j3 g y. k9 O, r4 w. g. u重点内容 " W' M4 v9 w- c& U- O7 s G+ N; z* Y * R V; m+ m5 [+ U7 s: u& {( G属性:5 d6 } E( V- ^6 t6 n
private static final Unsafe unsafe = Unsafe.getUnsafe();8 z, R, i3 w3 o, {: f5 O
; h9 i/ _2 ~5 r) q
private static final long valueOffset; : N5 H$ _; M, `3 ^ 4 l9 j8 q5 K0 e o9 bprivate volatile int value; 8 o3 x& m6 f6 O( P+ M+ |# e 5 J7 C2 y$ K7 U2 Y! t静态代码块:) k; \2 K; z D. R9 p9 p
static {' O' J! e2 P* l
try {& Q% O3 J% F' {3 J
valueOffset = unsafe.objectFieldOffset $ x* Q; C( T9 K4 B (AtomicInteger.class.getDeclaredField("value")); r! S( J u9 y" H! X } catch (Exception ex) { throw new Error(ex); } / `) s1 p7 Y1 G% J2 ^6 O2 t/ }& @ I" ~ }( D, R( ^8 S& m& `, `
--------------------- 3 z }% y ^! l$ B( e; C0 s4 n% A: f
构造方法 ' m7 d; ?6 L8 T2 ]8 _public AtomicInteger() { " ?5 b" f/ q& C- O7 R% n$ G }% D4 H; S' ?8 Z& e/ h2 A3 J! ^
1 F) P2 _- o- n' @8 Y, `
public AtomicInteger(int initialValue) { ) b7 T/ Q) Z- E. s Z4 D0 p value = initialValue; & D' Y! P3 P3 a$ f0 w# G9 l } # \9 J3 n6 [( r) o3 A; o1 O# X, q2 d
对象方法:5 R0 D* [. |" z" l, H& P! H% r
public final int get() { ( z9 j4 i1 T* {, B return value; 6 @( n1 a7 `, j$ v& L% V( D } 5 ]" B' v* s8 R; j9 [3 L: n 7 J- l0 J' K' m0 B* B) w& g3 x: K 9 l* ?0 F2 t8 n! mpublic final void set(int newValue) { - R, F9 v6 i# I5 k value = newValue; + Y; J/ E; e% G9 c } ; `7 n, o1 f9 Z6 Y1 k3 @% X; h5 d- O* n& W$ C7 i
* J, r5 \; Q6 A8 c
public final void lazySet(int newValue) {# C1 E* U% l) b6 b: E4 Z
unsafe.putOrderedInt(this, valueOffset, newValue); 4 d: t; s4 \- g0 H5 A& l# H }( y! _8 Y3 Z7 B$ w
' t: D$ [9 r' `1 _, x0 W/ V0 L3 z! C% I# Z
public final int getAndSet(int newValue) {) u8 A: J& ]' K: s8 a" b) ]
for (;;) { # E1 R5 [) C) C int current = get(); h, {( C' @' P7 J+ b1 d( v if (compareAndSet(current, newValue)) . h7 }, L) X" l. Q8 q return current;1 q+ t! A) H# k2 V0 W, e3 H
} ^+ g0 J( i1 @, k. y I$ S$ n
} % n( m% a1 p0 J" F5 d4 w' G6 q) d5 u# C
% ]6 y) A; s! L4 j* H' B) y% vpublic final boolean compareAndSet(int expect, int update) { 1 U" c( N5 b& l D- O2 S return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 4 m: y6 y/ e# `5 k. L8 o8 ?/ C } 6 s% Y w8 f9 T! p: U: g4 C 4 V5 Y! h; @* G 9 E5 ?( a6 x) y3 f) W+ T: W) E3 Zpublic final boolean weakCompareAndSet(int expect, int update) {5 t Y" H8 u# q5 Y
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);3 G0 _& b V# w
}1 ]6 X! |$ n4 [+ J" o& Y
4 d p" g# F$ E) O1 T2 R2 b0 ^( }
public final int getAndIncrement() { 5 x* V' S0 w5 ~4 ` for (;;) {- C! }7 U% N# L. k7 ^
int current = get(); * W0 k% S" E3 T7 [) T int next = current + 1;* m! j h! }. k; r% r
if (compareAndSet(current, next))" U0 y- O" C3 R% |: h1 M1 o6 G( A
return current; / @. |8 g. ~ A- G* [ } ' {1 Q$ ]$ M6 s0 K5 E( {2 z }8 }8 T4 H2 I/ U
9 c% L' T# x$ G8 j, N* p! q3 Y1 L' o' @0 Wpublic final int getAndDecrement() {6 ^8 T5 _' k' z. }5 l( o! {, o
for (;;) {1 @0 S+ s Z# G; L- Q# b
int current = get(); + B7 L, Q9 r3 C# M int next = current - 1; % U' t9 V1 p2 q+ @. o ~$ Q if (compareAndSet(current, next)) - a6 Z% Z3 M, a+ y return current; - l) r: ?0 c; Z } I" d0 W3 c" s* Z: o } F1 R/ z+ u% m$ \9 E" ?, f) N" u! N; q
7 h; r7 S! C) ~* j9 bpublic final int getAndAdd(int delta) {/ T. E: D& d2 E" C6 p
for (;;) { + a. S- F# J) z9 r int current = get();; A2 g& u9 t( E8 n1 S# o: A" \- K! f
int next = current + delta; 7 A1 p' c7 R8 R- e+ {% C) Z8 A if (compareAndSet(current, next)), A) V3 A5 W ~2 M+ t# O: d
return current;' f5 O# ?, O4 J* W' O9 U2 C" C: G
} ! _/ j9 `0 t# R) { } " b; r4 e7 o: i$ u. M8 h9 V8 l8 M T# b8 _7 I% t4 Y
1 H( w( E4 w# {$ g0 Fpublic final int incrementAndGet() { 7 L7 i' E+ e! `7 Z7 r. A( t5 G8 i for (;;) {5 f( O9 J4 ]3 `8 w
int current = get(); h2 I4 J) [4 E" j
int next = current + 1;: a8 Y& x- A: n/ Z8 S8 Y3 i+ A" b! x
if (compareAndSet(current, next))1 g, w/ F, S/ q: L# |# [, p
return next;- l: ?7 E5 l- P5 [: v1 s, W5 w: }
} 0 b; a1 D/ u! A a/ S5 D4 U }, R+ c, d5 E3 a) I7 P
, Q+ D+ ^ N8 W# D- Z8 {2 ^) u+ s% s0 w) E, F h
public final int decrementAndGet() { 1 E% j8 Z0 c4 o! o, i8 o: m for (;;) { 9 Z+ G) \% O' H7 U int current = get(); c# {# @2 ]: A' m: D int next = current - 1;7 S, A$ ^' T% P; a* I, ]( i
if (compareAndSet(current, next))1 E1 f1 l4 H# Y6 g9 Y1 m1 W
return next;; @7 U5 k" o+ f6 i* v
} 6 X5 s/ p' T6 h( w) z" y x }: Y; }- ~; m9 e4 k. ]
0 U4 L: ?0 Y/ K8 A* H- t$ v9 ] 7 |+ o: ?( s, S5 |- X0 |public final int addAndGet(int delta) { . X" D. p6 @/ l$ G v5 Z* U* f for (;;) {. J/ x* ~% v( y5 R
int current = get();; G' y) D; g; _5 c& w
int next = current + delta; ' x3 O+ Y' h6 P8 E1 Q if (compareAndSet(current, next))) g4 k8 H* j, I- m" o8 k, {# z* \& Q
return next; * T& _* l9 `* N$ q; \* _ ~5 | }1 W* X) ^; e; M# M7 z
}! ^! r& m: u; C# P
; @* }7 _8 i) Q# b A4 `: b# `! w: B- ]7 \9 S' _5 B6 V. _2 O3 G9 @
public String toString() { ( }4 O- j6 W% n" R7 C- F return Integer.toString(get()); , Q8 H: j8 U8 m$ A+ x. |- g- G) u }0 z4 m, ?9 y! e1 L0 r3 x/ O5 t
% P4 m9 t4 t3 B6 M' q5 G
& t! p8 Y! C1 b) I7 X
public int intValue() {8 }% E" Z K# U
return get(); ) n( ?) P; y0 E& A# B3 o, F: U }* R; y# U# o J. K6 x
* i8 ]1 Q( {$ V% D' |' z* C
; q3 t6 @+ Q2 E5 L- h. T4 ?( npublic long longValue() { # y6 A4 _; Q3 c, `9 P return (long)get();! v: W* _, ~$ Y! u1 Y, t/ @" i
}- J! N4 z6 c P8 u' c6 H
3 |+ J3 }( a' u. L& q' Y+ E. z6 N v2 |; ?, y. }
public float floatValue() {/ l- M: \9 u" q3 n4 l* i' ?) R8 j
return (float)get();3 s2 |* y& r; f" n" g5 Z) S* b' B6 D- L
}0 m, F6 k2 f- ]+ M
3 E2 ^5 S1 `0 A2 M y E
, M& }0 o# L# ^4 n7 F. |
public double doubleValue() { 4 h. m$ i. ] q/ R0 t# p* G return (double)get(); 8 W6 B7 X4 ]* S+ l4 t$ f1 V } - Z3 I% a: \* _1 n& W--------------------- - N& e' ?$ H( s8 e% a1 Q
作者:feyshine ; {: s) k( U; W$ R6 c
来源:CSDN 7 L T8 f) f2 Z$ C ?9 `# `! n* j/ G% J x$ W+ x
6 e4 c3 u' T! w1 f" x