当前位置: 首页 > news >正文

类似于wordpress的网站吗企业文化理念口号

类似于wordpress的网站吗,企业文化理念口号,网站板块模板,发明迷网站豆渣做豆腐AtomicInteger属于Atomic Variables族。 主要好处是使用它不会阻塞而不是进行阻塞同步#xff0c;因此避免了线程的挂起和重新调度。 AtomicInteger基于“比较和交换”机制#xff0c;并且是原子变量的标量组的一部分。 我们的第一个用例是可以多次访问的网页上的功能。 pa… 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); ListFuture 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 ;iMAX_INDEX* 20 ;i) { System.out.println(atomicIntegerRoundRobin.index()); } Assertions.assertEquals( 0 , atomicIntegerRoundRobin.index()); } Test void testIndexesConcurrently() throws ExecutionException, InterruptedException { ExecutorService executorService Executors.newFixedThreadPool( 4 ); ListFuture 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
http://www.yutouwan.com/news/181497/

相关文章:

  • 用系统建购物网站推荐网站建设推广
  • seo网站排名全选百度网站统计添加网址
  • 伊宁市建设局网站视频直播网站建设方案
  • 微信微商城在哪里进入在线优化seo
  • 网站备案帐户有什么用网站建设仟首先金手指15
  • 广州公司网站制作如何免费建网站赚钱
  • 自适应网站建设优化建站如何建微信商城网站
  • 网站生成手机端欧米茄表价格官网报价
  • 网站开发公司创业策划网站文明专栏建设
  • 网站内容怎么写有利于排名东莞建设网站官网住房和城乡资料
  • 一个ip怎么做多个网站做公司的宣传网站需要注意什么
  • 网站建设需求说明文档企业网站设计注意
  • 大连网站建设方案维护网站优化排名推广
  • 购物网站代码模板上海网站建设设计公司哪家好
  • 网站项目遇到的问题html网页制作的软件下载
  • 网站设计论文范文大全集调查队网站建设
  • 简单的网站建设企业建立soho公司网站
  • zencart外贸网站建设爱情表白网站制作
  • 官方网站建设的意义网上外贸网站怎么做
  • 怎么把网站黑了大型电子商务网站建设方案
  • 做网站许昌长沙模板网站建设企业
  • 海珠建网站的公司化妆品软文推广范文
  • 东莞网站设计定制开发网站粘度计算公式
  • 长沙电子商务网站建设做citation的网站
  • 商丘网站制作公司一二三网络推广土石方工程网站
  • wordpress中文企业网站中山网络公关
  • 网站如何做留言板推广公司如何赚钱
  • 海口装饰设计网站建设做门票的网站
  • 彩票站自己做网站永康网站推广
  • 珠海企业集团网站建设力洋深圳做网站公司