<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] # 簡介 使用原子的方式更新數組里的某個元素,可以確保修改數組中數據的線程安全性。 * AtomicIntegerArray:整形數組原子操作類 * AtomicLongArray:長整形數組原子操作類 * AtomicReferenceArray :引用類型數組原子操作類 上面三個類提供的方法幾乎相同,所以我們這里以 AtomicIntegerArray 為例子來介紹 # AtomicIntegerArray類常用方法 ~~~ public final int get(int?i)//獲取?index=i?位置元素的值 public final int getAndSet(int?i,?int?newValue)//返回 index=i 位置的當前的值,并將其設置為新值:newValue public final int getAndIncrement(int?i)//獲取?index=i?位置元素的值,并讓該位置的元素自增 public final int getAndDecrement(int?i)//獲取?index=i?位置元素的值,并讓該位置的元素自減 public final int getAndAdd(int?delta)//獲取?index=i?位置元素的值,并加上預期的值 boolean compareAndSet(int?expect,?int?update)//如果輸入的數值等于預期值,則以原子方式將?index=i?位置的元素值設置為輸入值(update) public final void lazySet(int?i,?int?newValue)//最終?將index=i 位置的元素設置為newValue,使用 lazySet 設置之后可能導致其他線程在之后的一小段時間內還是可以讀到舊的值 ~~~ # 示例 > 統計網站頁面訪問量,假設網站有10個頁面,現在模擬100個人并行訪問每個頁面10次,然后將每個頁面訪問量輸出,應該每個頁面都是1000次,代碼如下: ~~~ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicIntegerArray; public class AtomicExample7 { static AtomicIntegerArray pageRequest = new AtomicIntegerArray(new int[10]); //模擬訪問一次 public static void request(int page) throws InterruptedException { //模擬耗時5毫秒 TimeUnit.MILLISECONDS.sleep(5); //pageCountIndex為pageCount數組的下標,表示頁面對應數組中的位置 int pageCountIndex = page - 1; pageRequest.incrementAndGet(pageCountIndex); } public static void main(String[] args) throws InterruptedException { long starTime = System.currentTimeMillis(); int threadSize = 100; //100個形成一組 CountDownLatch countDownLatch = new CountDownLatch(threadSize); //開100個線程 for (int i = 0; i < threadSize; i++) { Thread thread = new Thread(() -> { try { //每個線程訪問10頁 for (int page = 1; page <= 10; page++) { //每頁10次 for (int j = 0; j < 10; j++) { request(page); } } } catch (InterruptedException e) { e.printStackTrace(); } finally { //每次都減少一次 countDownLatch.countDown(); } }); thread.start(); } countDownLatch.await(); long endTime = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName() + ",耗時:" + (endTime - starTime)); for (int pageIndex = 0; pageIndex < 10; pageIndex++) { System.out.println("第" + (pageIndex + 1) + "個頁面訪問次數為" + pageRequest.get(pageIndex)); } } } ~~~ 說明: > 代碼中將10個面的訪問量放在了一個int類型的數組中,數組大小為10,然后通過`AtomicIntegerArray`來操作數組中的每個元素,可以確保操作數據的原子性,每次訪問會調用`incrementAndGet`,**此方法需要傳入數組的下標,然后對指定的元素做原子+1操作**。輸出結果都是1000,可以看出對于數組中元素的并發修改是線程安全的。如果線程不安全,則部分數據可能會小于1000。
                  <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>

                              哎呀哎呀视频在线观看