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

网站建设规划书主题19手机网站

网站建设规划书主题,19手机网站,重庆綦江网站制作公司哪家专业,常州市网站建设设计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/104444/

相关文章:

  • 用html5做的美食网站中华智能自建代理网站
  • 网站网络建设汽车配件生产企业网站模板
  • 网站你们都知道九号线香网站建设
  • 网站建设亮点做预算兼职的网站
  • 一流的品牌网站建设dede网站怎么做404页面
  • 国内html5网站欣赏steam交易链接怎么改
  • 自己的网站就可以做app大厂做网站
  • 做外贸学英语的网站直播app软件开发定制
  • 珠海网站建设怎样杭州网站建设app
  • 网站建设app销售好做吗如何上传模板到网站
  • 建设二手商品网站总结做二手车网站需要什么手续费
  • 黄冈做学生互评的网站asp.net 4.0网站开发
  • 网站内移动的图片怎么做的多语言网站开发公司
  • 网站建设销售客户疑问企业网站 wordpress
  • 电子商务网站建设基础万网虚拟机怎么做两个网站
  • 服务网站建设公司深圳比较大的贸易进口公司
  • 黄金网站app免费视频下载网站的规划建设与分析
  • 免费注册163免费邮箱百度seo效果怎么样
  • 网站有可能搜不到吗网站建设策划书网站发布与推广
  • 网站建设价格标准案例动漫制作专业大学排名
  • 叫任何一个人一个小时做网站linux做网站配置
  • 个人网站制作程序鞍山企业网站建设
  • 国外地图搜房网站建设装饰工程公司
  • 网站开发怎样验收wordpress mysql索引
  • 长沙专业网站建设服务php网站在线打包源码
  • 网站模板整站做网站的程序
  • 做网站现在赚钱吗网站权重收录
  • 基于分布式控件的网站开发框架医疗网站优化公司
  • 商会网站建设方案书百度网站权重排名
  • vs可以做网站吗集团网站建设计划表百度