<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ![](https://cdn.zimug.com/wx-zimug.png) AtomicInteger 類底層存儲一個int值,并提供方法對該int值進行原子操作。AtomicInteger 作為`java.util.concurrent.atomic`包的一部分,從Java 1.5開始引入。 ## 1. AtomicInteger基礎用法 通過下文的`AtomicInteger`構造方法,可以創建一個`AtomicInteger`對象,該對象的初始值默認為0。`AtomicInteger`提供get和set方法,獲取底層int整數值,與設置int整數值 ~~~ //初始值為0的atomicInteger對象 AtomicInteger atomicInteger = new AtomicInteger(); //初始值為200的atomicInteger對象 AtomicInteger atomicInteger = new AtomicInteger(200); int currentValue = atomicInteger.get(); //100 atomicInteger.set(2453); //現在的值是 2453 ~~~ 但是上面的方法,對于`AtomicInteger`而言并不是它的核心內容,`AtomicInteger`核心內容體現在它的原子性,我們下文介紹。 ## 2. 什么時候需要使用AtomicInteger 我們通常在以下的兩種場景下使用`AtomicInteger` 1. 多線程并發場景下操作一個計數器,需要保證計數器操作的原子性。 2. 進行數值比較,如果給定值與當前值相等,進行數值的更新操作,并實現操作的非阻塞算法。 #### 2.1. 原子計數器場景 把`AtomicInteger`作為一個計數器使用,`AtomicInteger`提供了若干方法進行加法、減法的原子操作。 > 比如從一個map里面獲取值,用get()方法,這是第一個操作;獲取到值之后給這個值加上n,這是第二個操作;將進行過加法運算的值,再次放入map里面是第三個操作。所謂操作的原子性是指:在多線程并發的場景下,上面的三個操作是原子性的,也就是不可分割的。不會出現A線程get了數值,B線程同時也get到了該數值,兩個線程同時為該值做運算并先后再次放入的情況,這種情況對于`AtomicInteger`而言是不會出現的,`AtomicInteger`操作是線程安全的、不可分割的。 * `addAndGet()`- 將給定的值加到當前值上,并在加法后返回新值,并保證操作的原子性。 * `getAndAdd()`- 將給定的值加到當前值上,并返回舊值,并保證操作的原子性。 * `incrementAndGet()`- 將當前值增加1,并在增加后返回新值。它相當于`++i`操作,并保證操作的原子性。 * `getAndIncrement()`- 將當前值增加1并返回舊值。相當于`++i`操作,并保證操作的原子性。 * `decrementAndGet()`- 將當前值減去1,并在減去后返回新值,相當于`i--`操作,并保證操作的原子性。 * `getAndDecrement()`- 將當前值減去1,并返回舊值。它相當于 `--i`操作,并保證操作的原子性。 下面是AtomicInteger原子性操作方法的例子 ~~~ public class Main { public static void main(String[] args) { //初始值為100的atomic Integer AtomicInteger atomicInteger = new AtomicInteger(100); System.out.println(atomicInteger.addAndGet(2)); //加2并返回102 System.out.println(atomicInteger); //102 System.out.println(atomicInteger.getAndAdd(2)); //先獲取102,再加2 System.out.println(atomicInteger); //104 System.out.println(atomicInteger.incrementAndGet()); //加1再獲取105 System.out.println(atomicInteger); //105 System.out.println(atomicInteger.getAndIncrement()); //先獲取105再加1 System.out.println(atomicInteger); //106 System.out.println(atomicInteger.decrementAndGet()); //減1再獲取105 System.out.println(atomicInteger); //105 System.out.println(atomicInteger.getAndDecrement()); //先獲取105,再減1 System.out.println(atomicInteger); //104 } } ~~~ #### 2.2. 數值比對及交換操作 compareAndSet操作將一個內存位置的內容與一個給定的值進行比較,只有當它們相同時,才會將該內存位置的內容修改為一個給定的新值。這個過程是以單個原子操作的方式完成的。 compareAndSet方法:如果`當前值==預期值`,則將值設置為給定的更新值。 ~~~ boolean compareAndSet(int expect, int update) ~~~ * expect是預期值 * update是更新值 AtomicInteger compareAndSet() 方法的例子 ~~~ import java.util.concurrent.atomic.AtomicInteger; public class Main { public static void main(String[] args) { //初始值為100的atomic Integer AtomicInteger atomicInteger = new AtomicInteger(100); //當前值100 = 預期值100,所以設置atomicInteger=110 boolean isSuccess = atomicInteger.compareAndSet(100,110); System.out.println(isSuccess); //輸出結果為true表示操作成功 //當前值110 = 預期值100?不相等,所以atomicInteger仍然等于110 isSuccess = atomicInteger.compareAndSet(100,120); System.out.println(isSuccess); //輸出結果為false表示操作失敗 } } ~~~ ## 3. 總結 `AtomicInteger`可以幫助我們在不使用synchronized同步鎖的情況下,實現在多線程場景下int數值操作的線程安全,操作的原子性。并且使用`AtomicInteger`來實現int數值的原子操作,遠比使用synchronized同步鎖效率更高。 `java.util.concurrent.atomic`包不僅為我們提供了`AtomicInteger`,還提供了AtomicBoolean布爾原子操作類、AtomicLong長整型布爾原子操作類、AtomicReference對象原子操作類、AtomicIntegerArray整型數組原子操作類、AtomicLongArray長整型數組原子操作類、AtomicReferenceArray對象數組原子操作類。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看