Java和Round-Robin上的AtomicInteger
生活随笔
收集整理的這篇文章主要介紹了
Java和Round-Robin上的AtomicInteger
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AtomicInteger屬于Atomic Variables族。 主要好處是使用它不會阻塞而不是進行阻塞同步,因此避免了線程的掛起和重新調度。
AtomicInteger基于“比較和交換”機制,并且是原子變量的標量組的一部分。
我們的第一個用例是可以多次訪問的網頁上的功能。
package com.gkatzioura.concurrency; import java.util.concurrent.atomic.AtomicInteger; public class AtomicIntegerExample { private AtomicInteger atomicInteger = new AtomicInteger(); public void serveRequest() { atomicInteger.incrementAndGet(); /** * logic */ } public int requestsServed() { return atomicInteger.get(); } }并測試我們的用例
package com.gkatzioura.concurrency; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class AtomicIntegerExampleTest { private AtomicIntegerExample atomicIntegerExample; @BeforeEach void setUp() { atomicIntegerExample = new AtomicIntegerExample(); } @Test void testConcurrentIncrementAndGet() throws ExecutionException, InterruptedException { final int threads = 10 ; ExecutorService executorService = Executors.newFixedThreadPool(threads); List<Future> futures = new ArrayList(); for ( int i = 0 ; i { atomicIntegerExample.serveRequest(); return null ; })); } for (Future future: futures) { future.get(); } Assertions.assertEquals( 10 ,atomicIntegerExample.requestsServed()); } }除了使用原子整數作為計數器之外,您還可以在各種情況下使用它。 例如線程安全的循環算法。
package com.gkatzioura.concurrency; import java.util.concurrent.atomic.AtomicInteger; public class AtomicIntegerRoundRobin { private final int totalIndexes; private final AtomicInteger atomicInteger = new AtomicInteger(- 1 ); public AtomicIntegerRoundRobin( int totalIndexes) { this .totalIndexes = totalIndexes; } public int index() { int currentIndex; int nextIndex; do { currentIndex = atomicInteger.get(); nextIndex = currentIndex< Integer.MAX_VALUE ? currentIndex+ nextIndex = currentIndex< Integer.MAX_VALUE ? currentIndex+ 1 : 0 ; } while (!atomicInteger.compareAndSet(currentIndex, nextIndex)); return nextIndex % totalIndexes; } } totalIndex是索引的總數。 當請求下一個索引的請求時,計數器將增加,并進行比較和設置操作。 如果由于另一個線程而失敗,則它將再次嘗試該操作,并將獲得計數器的下一個值。
模運算將給出當前索引。 如果原子整數達到最大值,則應將其重置為零。 重置會導致邊緣情況并更改索引的順序。 如果這是一個問題,則可以根據總索引大小來調整最大值以避免這種情況。
還對此進行了一些測試。
package com.gkatzioura.concurrency; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; AtomicIntegerRoundRobinTest { class AtomicIntegerRoundRobinTest { private static final int MAX_INDEX = 10 ; private AtomicIntegerRoundRobin atomicIntegerRoundRobin; @BeforeEach void setUp() { atomicIntegerRoundRobin = new AtomicIntegerRoundRobin(MAX_INDEX); } @Test void testIndexesSerially() { for ( long i= 0 ;i<MAX_INDEX* 20 ;i++) { System.out.println(atomicIntegerRoundRobin.index()); } Assertions.assertEquals( 0 , atomicIntegerRoundRobin.index()); } @Test void testIndexesConcurrently() throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool( 4 ); List<Future> futures = new ArrayList(); for ( int i = 0 ; i atomicIntegerRoundRobin.index())); } for (Future future: futures) { System.out.println(future.get()); } Assertions.assertEquals( 0 ,atomicIntegerRoundRobin.index()); } }翻譯自: https://www.javacodegeeks.com/2019/11/atomicinteger-on-java-and-round-robin.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java和Round-Robin上的AtomicInteger的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想玩红警却不知道怎么下载如何在电脑上下载
- 下一篇: 制定目标会让你陷入思维盲点导致任务失败,