AtomicInteger类! i7 J# I8 W) J; m
. p7 \/ N! U+ n
这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。 : h! x/ ~9 \% \4 Z 2 j! u3 j1 t3 M! A7 D重点内容 ; @2 R }) g! ~6 ^* z0 {( m* g6 N' K6 _6 W9 C% U4 h2 c
属性:* N0 m8 M# B, A! X8 h
private static final Unsafe unsafe = Unsafe.getUnsafe();5 u2 X4 |5 ?/ b- W
6 x8 T/ }$ w; j( Q1 `6 _- cprivate static final long valueOffset; 0 Z# U5 S8 g0 g* H9 V 9 U. C8 o9 t( g) @0 pprivate volatile int value;. O) j! Y+ O% C, Y9 p/ x4 [7 l
# s7 d+ k# [3 Z9 W' z$ Z静态代码块: ; t8 v* X+ V* x0 y7 {static {& T5 M" Y. _, Q
try {4 B1 s9 [8 Z: N0 T- ^; w A
valueOffset = unsafe.objectFieldOffset+ P9 ~& g2 h( M4 a1 {
(AtomicInteger.class.getDeclaredField("value")); M3 m5 e0 M) A: t" e5 B) L' ]& ], O
} catch (Exception ex) { throw new Error(ex); }% A# h* n" Q+ w: p1 h2 n
}7 a+ y* l6 }+ ?* b
--------------------- ) v0 Q/ ]7 Q7 l- ^. H, ]构造方法 2 [" O4 b/ W: m6 v/ [+ dpublic AtomicInteger() {9 ^1 x( a' l; T1 i
} . n; m; g1 y6 _5 B5 H - V/ G ^6 a, m$ O6 }1 o" \public AtomicInteger(int initialValue) { / p; O' K- j9 |/ j( n& ? value = initialValue;/ O9 q. F) E7 r, T
} , e/ w8 ^( t5 t5 L1 f" L9 Q) H7 K' f1 \ m( N; ?& X' W
对象方法: / \5 C1 I1 \0 I public final int get() {8 Q, w, _: T6 ]. e$ P# t% j) M
return value; 2 h5 I/ a# q$ a. N" t } 2 K h5 y7 `4 d) C6 R/ ~% _" k8 @) U- \9 F9 N* p
# @( \5 G6 d' T$ t M: k( |3 u
public final void set(int newValue) {, ?# w8 u0 q4 K' G' G
value = newValue; ! Q$ u# Y% s+ L# C9 Z |, \ }4 e/ U& |% P. g: d% T
* M9 g1 v- |2 F
- m4 W. r# o* i# ~! t3 J3 R& u
public final void lazySet(int newValue) {0 R5 e9 Y* g& J9 Z: f
unsafe.putOrderedInt(this, valueOffset, newValue); z M) n0 Y& D7 L& i6 j }: v% ^7 m8 z4 G6 S9 R! Q. D
8 b: I( i2 H! r9 H* `. R1 u9 E; b
* f8 @7 u/ q7 j/ ~6 X6 Wpublic final int getAndSet(int newValue) { N! G+ \% h7 t* K( R" v4 D$ { for (;;) {4 ~1 R" Z9 Z& o# [6 f# }' }
int current = get();+ u6 R3 W }6 _3 |: [. e) }- O
if (compareAndSet(current, newValue)) 9 V* H1 }4 l3 y' u) N( z. i$ z return current;/ C0 R z5 M8 Q# _
} ! G$ [& O8 O9 v, { } 3 A4 f1 w1 M s4 s3 m, a3 |( Y 1 X& l( ~* ~% p: [8 u- X1 i& Z1 q$ j- I2 ?
public final boolean compareAndSet(int expect, int update) { e* B# M4 K8 X3 o2 z
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); # P( t. y% t x+ J2 P; `0 ~ }. E ]6 c5 y, V- ^4 G. b! x
4 c% m7 Z8 _1 ~/ g; b
+ r* h+ u6 n8 o6 J* Kpublic final boolean weakCompareAndSet(int expect, int update) {& o5 V+ l% x( B6 Q n
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); 9 w7 P6 R0 ?3 N, {- J! e7 y }3 ~3 `* K) F9 |, h+ f
( M* |+ [1 t+ V! Z9 |1 t! ?public final int getAndIncrement() {6 L+ n0 F6 H# N# j3 ^
for (;;) { . ^; r- g8 [2 ]: b0 d int current = get(); % L( X* y2 _: V# Y/ y3 K, ] int next = current + 1; ' V# l+ J/ m0 z; q if (compareAndSet(current, next)) - _2 Z, }3 B1 Z# ^ return current; 4 U. u$ G) u* \% F4 ~ }# y0 V6 `+ Q( C, }* u. f8 C+ ]
}3 Z" I, w/ b/ u! N, V8 q- K
# O. Q. q3 Q+ k c' dpublic final int getAndDecrement() { 0 \4 V4 j7 K, C. m- r" V for (;;) {+ P; [8 N9 ?4 S3 N% _8 G
int current = get(); 8 l$ s* E* J; d3 q/ z/ q int next = current - 1;9 _. i7 i8 K( F+ l' M
if (compareAndSet(current, next)), q) r/ [3 B; ?, }$ `0 q
return current; 5 S; [6 `% |; X( v$ R- U* Q! ` } 3 g( k0 D; w9 _ } " J2 {: X& N$ f% y5 {) v 4 O( Y& G8 K* x4 q. F 5 M. I0 O4 E1 e3 B: k$ Z# upublic final int getAndAdd(int delta) { % w; ~* I4 r v! E) J for (;;) {$ ~( V' ?! E" B( S' ?/ q5 B
int current = get(); * m P! A5 H9 {/ ] int next = current + delta; ; r$ f; z1 ^" I: F% p if (compareAndSet(current, next)) * } n5 _# N7 D9 n( l- M- F return current; 0 k$ w+ ?' X, ]3 X6 F }: O, \" z( M& H5 b# _: S b$ v
}6 f1 U# e1 u( e5 m; [
9 ^' B+ X5 g$ c D3 U% ]; c
7 Q" b6 _7 i2 Q& W3 e1 y
public final int incrementAndGet() { # y. I' {7 w$ {" y for (;;) {# Y+ M* l; Q" q2 A$ B2 m0 V1 d
int current = get(); 7 c& f7 M. S* z# ^% u D int next = current + 1; $ J( ]7 M! d* e, f$ t if (compareAndSet(current, next)) + H2 F8 G. g1 ]8 w3 Y7 S return next;9 `! ? l0 ~+ V) ]6 e* k
} & S1 T2 h# C$ P6 ? } / Y( w* X5 `1 q# r5 y& J/ `* P% r; F1 i/ v2 K: f. \$ }
1 x, _$ ]( f" y
public final int decrementAndGet() { % _( Y9 Z' K+ E5 `1 x# l4 k for (;;) {3 S: A9 V0 `8 ?) W
int current = get();' F% j5 Q# n) `6 @5 @+ P
int next = current - 1; 3 ~" h; b+ W; e if (compareAndSet(current, next)) ' q+ [- v/ B9 _# R return next; # ~/ t. B, u+ B& a1 [6 E } . I# @+ e. ]5 w) \ }, t4 D9 B( c# [3 u' P. T" n S/ G
3 B4 G3 I) d7 R/ q* W1 T1 q: l* M: V: a
public final int addAndGet(int delta) { & O g+ O) ~2 n, g2 W) I: k$ V for (;;) { _& ~, d" d8 g; r# _$ W2 A" Q' T
int current = get();5 r6 f3 W+ `3 D$ F' }6 s2 `
int next = current + delta;+ X! G3 Y" o0 @6 g
if (compareAndSet(current, next)) ! X" Z; R% O$ T$ d! M return next; T4 U7 C0 P- y4 p }3 B6 T$ W, P$ Z; L1 v/ j# V
}% [4 I, H: N9 B# W
9 m B* s, v' `7 g1 S
/ m/ m. i( R/ d- {" Q
public String toString() { 2 A2 N" L! {# |1 p+ Z return Integer.toString(get()); 9 t9 N7 W$ G) |: ` }1 ?8 C" m- g+ M* j! q# V* M+ G2 S& z
. ]3 D1 f+ x+ B# {2 v) ]6 T9 K; E1 Y7 V; _& v+ O8 I
public long longValue() {$ H+ j* \# F1 c/ } k
return (long)get(); 0 |, @+ u) A1 A- k& `; K }/ o, u2 s! A* L" T4 D U- e
3 V9 t5 O0 S- z- t 1 L( b T- T2 g( ~public float floatValue() {. y! w2 ^. E( |& _( I
return (float)get(); # I( A( f* @+ m) o( t" O& x }& X5 O5 Y+ N' e6 J. g) Z6 z4 e
, \- m* V9 m, H6 y# T
$ B- y2 `9 N [6 D& v, w0 ?
public double doubleValue() { 1 i7 s6 j! x* c0 p9 j! } return (double)get(); . F o* E" P8 G! V! w+ s } 5 B/ U" R; U# @# `--------------------- 4 u }; }" b" L+ H* J+ V作者:feyshine % d/ ~2 O# }1 W8 k6 R# X
来源:CSDN 0 }6 M8 p' B* D4 w9 d2 C- F) S 7 G3 k" ]5 s5 H4 ]2 s5 ~6 \ H1 l' b5 f* v8 x