数学建模社区-数学中国

标题: AtomicInteger类 [打印本页]

作者: 杨利霞    时间: 2019-3-23 16:04
标题: AtomicInteger类
AtomicInteger类* O! a, g7 G  t7 B1 u3 V

4 ^  y+ }; s7 D" p+ k! X这一讲,主要讲讲AtomicInteger类,此类同样是Number的子类,同时实现 Serializable。
0 W  \2 |# w" a$ x. s' p7 N1 V0 |1 p( g) R
重点内容' R# m& H* B# j6 @

8 H9 y. _. m5 x9 O4 i$ \" N7 |属性:* o+ _+ Q4 h& i$ B
private static final Unsafe unsafe = Unsafe.getUnsafe();" y" r$ [; O/ }

  i( r, h! i2 e7 z4 C: I4 ^& @private static final long valueOffset;7 [& o& h- Q4 \4 M5 J; {% C9 g7 Z4 Q1 X
! N# l* \4 c8 p# T/ ?
private volatile int value;
" M. t( I$ e/ {7 H, Z: D3 x7 b% h# g! u' v- t  G/ |
静态代码块:8 C& v. Z3 D2 E. {
static {
$ t: V( @" a) K- I* g6 ^        try {
' a+ h6 B8 g0 `4 P! J1 L/ h            valueOffset = unsafe.objectFieldOffset! g) l7 j. v/ T9 ~* [; \0 s
                (AtomicInteger.class.getDeclaredField("value"));; p& V9 p$ i# u& P( a
        } catch (Exception ex) { throw new Error(ex); }
! J1 M$ d, t* A3 K! p1 o5 y, U    }
% |& l8 _8 d* b2 W---------------------
4 ?9 X6 d# e" P4 p5 h  @构造方法
6 O$ }$ T* n, o# I7 @public AtomicInteger() {
6 T/ E7 z* {" D+ W! \6 I4 \; A. }    }5 j( c3 _3 ?1 O# A  l9 g! n2 ~# O5 ^" w

1 H+ L) w+ o9 |$ S5 H, o" Opublic AtomicInteger(int initialValue) {* n4 k7 u% r' o; m8 h8 R
        value = initialValue;$ k3 r$ ~2 b& c! J
    }
) w& b0 ]) G+ a: h& z- S0 ]3 t* b( j: T# I& M1 I
对象方法:7 V( X+ Z5 p: q7 F/ M0 D; p; u1 a; ~
public final int get() {
" {/ y7 M1 N5 j1 H$ H+ b  o8 k5 r        return value;
1 k) d2 o! T( K# v    }
7 {* e0 V" @. \$ [* B( y9 v9 c' R/ N' C9 O
/ s; @% P' ^) ?: E
public final void set(int newValue) {. j, b# \. w/ v/ t
        value = newValue;
; J: T9 a) M( T* W! T& j8 f    }: j5 w8 k5 B& c# f: I& s9 W7 Q4 o
( i& o1 D- ?: ]# D2 z" ~8 M
. Z5 a6 |5 Y. I* P& J
public final void lazySet(int newValue) {# P1 [1 I1 q2 @: S/ k6 [9 T* q
        unsafe.putOrderedInt(this, valueOffset, newValue);
+ ~1 F2 y5 e, G2 n    }
6 h; n* ?, _1 ?+ O/ l! ]3 t2 f6 D8 \% t2 `9 o$ O3 L! t
# g# ?2 o. U% Q# p
public final int getAndSet(int newValue) {' s$ q- i* }1 S0 @" W; d- X
        for (;;) {
7 Z! t( m3 u1 O/ ]" A4 s3 }            int current = get();$ W$ n- a8 g9 {+ f
            if (compareAndSet(current, newValue))1 a) F$ i/ I6 c* U5 X9 f4 n
                return current;7 u& l2 j2 W& {3 L/ T. K% s
        }
" t: B7 e8 J( E! ]  ]5 Y    }+ k* S  \- q' [! I
3 g; B/ j  d5 e

& y8 T( s* d; j' `  {( _! Tpublic final boolean compareAndSet(int expect, int update) {
' l% S2 k$ U8 L0 N1 g% e        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);) X8 n. K% Q& X& {' a, k2 E
    }2 L  d0 [1 u5 ?/ I+ b
, }3 H% o  y4 c6 O7 U: r4 y

% m( ]% u" |% l- U9 V( b3 xpublic final boolean weakCompareAndSet(int expect, int update) {
2 G2 J  o" H  }, L2 ]+ g1 i        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);( v9 ], u0 }) A# ~' j  h2 k# X
    }
0 _* n5 G# F6 a- R" O9 Q) {6 h# a% q* Y9 {2 M6 L- N! F4 M
public final int getAndIncrement() {  k; T7 }7 x6 ]) h0 m' M
        for (;;) {$ x7 t6 B3 _$ b. ^) o: Q
            int current = get();0 U" U% V7 u) m, V% x- E6 J
            int next = current + 1;0 O% H8 |! k% @% I( y
            if (compareAndSet(current, next))
% Y7 V, s$ `# I6 h  A                return current;1 b4 z' B. x4 g- B: p  P' I* o
        }$ Y# l* q1 j, k) N
    }4 N) |. y) h. [3 [1 b. `

1 M0 [8 C2 j" Dpublic final int getAndDecrement() {
! a* N+ o- O, T7 q/ T        for (;;) {5 |) M/ M* L9 v% S- }1 z) O
            int current = get();
0 o6 w6 y2 ~- M* d: S( H/ k/ ]5 Q- s            int next = current - 1;2 p$ r+ T  R- q6 b
            if (compareAndSet(current, next))
" I; A( a1 D5 g( D/ k                return current;) n( j. Z& h0 N" Z
        }' \1 D4 v/ _. B" C
    }* G; v8 U9 D+ T6 Y
+ K1 W$ c1 w7 R+ \" \4 E

, B2 |, k% C/ U6 C1 Ypublic final int getAndAdd(int delta) {" @# a$ b3 d6 m: J: v, P! ?: l! h
        for (;;) {$ s1 v. F8 K6 G. h. _
            int current = get();
) h9 A/ r' h! o4 ?: ^            int next = current + delta;! s3 Q% ]  v/ ]
            if (compareAndSet(current, next))
4 ~8 t0 Q! o/ k# T% k                return current;
5 U/ b% c& c' s" a% n  K        }
7 k( [9 E( t6 D/ h3 j    }
3 J1 X$ c( M" X) p- u1 ?6 Z. k
: S$ O  |8 ?; r* }* Q$ E) |( j# T  ~" ?! F
public final int incrementAndGet() {% X3 s6 t) d0 @; h$ A
        for (;;) {
& i1 k6 e$ [. d4 ^; M/ H! {+ f9 \            int current = get();3 W1 e# i4 `2 q( w. Q
            int next = current + 1;! _; }3 ~9 S5 @
            if (compareAndSet(current, next))
$ X- K$ N" m, _3 L0 j# I, ]4 s                return next;
7 b- J" N/ S1 U' h2 A5 r, c6 ]6 X# y        }
( k" Z5 \( ]: O+ h    }
1 t# k& |4 M7 ~' h3 h1 O( h5 |2 p2 T/ ]$ F: z; E) O# v0 ?+ j0 y" `

7 r; C( c6 k; h6 T7 L+ gpublic final int decrementAndGet() {
' q9 Q- V. W0 _  f        for (;;) {: {6 O" T# f" c0 l% N( Z
            int current = get();3 K( O; B. S9 q8 i8 u, T! J3 V
            int next = current - 1;
! f. c3 O2 P$ K& H- c7 j* \            if (compareAndSet(current, next))
: J) v& E) ?9 z& p# r/ K9 N                return next;2 G/ x: \1 d' ?" M! V. I  l; _% l- {! j
        }9 ?& ~/ [0 M2 }. _; @7 s0 }
    }! c3 N, v- w% V+ H  s& ^5 W/ }
' G% y( J; ~- Z. }: o

4 v, b& C) v. b9 dpublic final int addAndGet(int delta) {& _7 X& r8 ?# P  j
        for (;;) {/ L6 u/ z& H$ E3 P4 B
            int current = get();  C$ S5 @) G/ N* x6 B4 [, m/ @
            int next = current + delta;; ?' ?/ e) t( T1 l5 O# J/ s
            if (compareAndSet(current, next))
. l; W* [4 j# l" S! N                return next;
  z; j! d& z' i/ f5 Q7 O9 h: N        }
; S; U. Y! }+ N8 z- C    }
- \) ?0 Y9 F7 w: |* A( c7 O) m( p6 E9 M2 O( ]

$ X; `' _- [" d! Q" Jpublic String toString() {
! _8 k2 |( Z' O; E, l" z( `$ l        return Integer.toString(get());
/ _4 I) @. G9 y, d% h9 N    }, ?4 T: J, o$ S

, t) V3 W9 m: C$ |  \  [# a6 v) N& B1 j- U
public int intValue() {; n, o5 \% p4 |( E
        return get();% X" ^. ^+ s0 G& \
    }+ l0 m1 h1 {% g5 f/ o  E( `8 y$ x: L
9 l( `8 O" T% r, g; }/ g) |
* j6 E6 d! v6 O& K
public long longValue() {
" E) `$ C) a2 f+ E# T5 u* Y  _        return (long)get();% H. L" P* Z. L9 A% m( r
    }
, I0 u, B& b( D1 ]3 B( U9 @1 ^- _3 R, Y* t
7 D7 z( p4 g8 b9 B  G9 F8 |
public float floatValue() {
0 D3 q" Y/ `) p0 V8 s" [/ O* S        return (float)get();
7 J, j4 l+ w- t. f2 {    }
+ E6 y6 x$ I0 z) a  z! T3 N0 F8 N4 ?4 \( n: }5 V
3 ?  p% G+ m' X; z8 s, s3 R% m
public double doubleValue() {+ ?" N4 d* t7 l
        return (double)get();
% K3 J' W; S' B; V- B% K; T( @/ H; S    }% c+ W0 u7 x/ N; p% E
---------------------
& H1 k( c4 F/ v$ t7 h  q1 O0 ~% q' b作者:feyshine
1 u. T& _3 b. b3 Z/ x* U9 R8 {来源:CSDN
; c* F, V6 S% l7 E) [5 x( O2 C8 J  k, r) X& s- |8 q/ Q

( F; B2 P5 E9 _% x4 c+ I( b! b( m+ K/ q3 j5 m
7 f; P8 W6 J0 d: a) ?0 N

: P$ b& w( u4 f- E7 A5 _

16种常用的数据分析方法汇总.docx

20.53 KB, 下载次数: 0, 下载积分: 体力 -2 点






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5