<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 簡介 使用原子的方式更新基本類型 * AtomicInteger:int類型原子類 * AtomicLong:long類型原子類 * AtomicBoolean :boolean類型原子類 上面三個類提供的方法幾乎相同,這里以 AtomicInteger 為例子來介紹 # AtomicInteger 類常用方法 ~~~ public final int get()//獲取當前的值 public final int getAndSet(int?newValue)//獲取當前的值,并設置新的值 public final intgetAndIncrement()//獲取當前的值,并自增 public final intgetAndDecrement()//獲取當前的值,并自減 public final intgetAndAdd(int?delta)//獲取當前的值,并加上預期的值 boolean compareAndSet(int?expect,?int?update)//如果輸入的數值等于預期值,則以原子方式將該值設置為輸入值(update) public final voidlazySet(int?newValue)//最終設置為newValue,使用 lazySet 設置之后可能導致其他線程在之后的一小段時間內還是可以讀到舊的值 ~~~ # 部分源碼 ~~~ private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final long valueOffset; static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) { throw new Error(ex); } } private volatile int value; ~~~ > 2個關鍵字段說明: > **value**:使用volatile修飾,可以確保value在多線程中的可見性。 > **valueOffset**:value屬性在AtomicInteger中的偏移量,通過這個偏移量可以快速定位到value字段,這個是實現AtomicInteger的關鍵。 **getAndIncrement源碼:** ~~~ public?final?int?getAndIncrement()?{??? return?unsafe.getAndAddInt(this,?valueOffset,?1); } ~~~ 內部調用的是**Unsafe**類中的**getAndAddInt**方法,我們看一下**getAndAddInt**源碼: ~~~ // var1是this, var4是1, var2是舊的預期值 public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { //var5是新的真實值 var5 = this.getIntVolatile(var1, var2); } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; } ~~~ ~~~ 說明: this.getIntVolatile:可以確保從主內存中獲取變量最新的值。 compareAndSwapInt:CAS操作,CAS的原理是拿期望的值和原本的值作比較,如果相同則更新成新的值,可以確保在多線程情況下只有一個線程會操作成功,不成功的返回false。 上面有個do-while循環,compareAndSwapInt返回false之后,會再次從主內存中獲取變量的值,繼續做CAS操作,直到成功為止。 getAndAddInt操作相當于線程安全的count++操作,如同: synchronize(lock){ ? ?count++; } count++操作實際上是被拆分為3步驟執行: 1. 獲取count的值,記做A:A=count 2. 將A的值+1,得到B:B = A+1 3. 讓B賦值給count:count = B 多線程情況下會出現線程安全的問題,導致數據不準確。 synchronize的方式會導致占時無法獲取鎖的線程處于阻塞狀態,性能比較低。CAS的性能比synchronize要快很多。 ~~~ # 例子 ~~~ @Slf4j public class AtomicExample1 { // 請求總數 public static int clientTotal = 5000; // 同時并發執行的線程數 public static int threadTotal = 200; public static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { executorService.execute(() -> { try { semaphore.acquire(); add(); semaphore.release(); } catch (Exception e) { log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("count:{}", count.get()); } private static void add() { count.incrementAndGet(); // count.getAndIncrement(); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看