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

哈尔滨市住房和城乡建设局网站专注网站建设怎么样

哈尔滨市住房和城乡建设局网站,专注网站建设怎么样,慈溪建设集团网站,ssh做电商 网站目录 使用单线程使用多线程使用多线程 synchronized使用多线程 原子类AtomicLong 使用单线程 单线程修改计数器的值#xff0c;没有发生问题#xff0c;每次运行结果都是10000#xff0c;不过程序耗时较长 package com.example;/*** 计数器*/ class Counter {private st… 目录 使用单线程使用多线程使用多线程 synchronized使用多线程 原子类AtomicLong 使用单线程 单线程修改计数器的值没有发生问题每次运行结果都是10000不过程序耗时较长 package com.example;/*** 计数器*/ class Counter {private static long count;public static long getCount() {return count;}public static void incrementCount() {count;} }public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0for (int i 0; i 10000; i) {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}count Counter.getCount();System.out.println(count);// 10000} } 使用多线程 单线程修改计数器的值运行速度提高了不过运行结果每次都不一致而且结果不是10000 package com.example;import java.util.ArrayList; import java.util.List;/*** 计数器*/ class Counter {private static long count;public static long getCount() {return count;}public static void incrementCount() {count;} }public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);} }执行结果 第一次9910 第二次9912 第三次9910使用多线程 synchronized 多线程加锁后最后结果都是10000 package com.example;import java.util.ArrayList; import java.util.List;/*** 计数器*/ class Counter {private static long count;public static long getCount() {return count;}public static synchronized void incrementCount() {count;} }public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);} } 执行结果 第一次10000 第二次10000 第三次10000使用多线程 原子类AtomicLong 多线程中使用原子类AtomicLong实现计数器最后结果都是10000 原理是CASCompare and Set 先比较原始值和预期值如果相等则修改为新值不相等则修改失败 伪代码如下 bool compareAndSet(oldValue, expectValue, updateValue){if(oldValue expectValue){oldValue updateValue// update success} else{// update fail} }package com.example;import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicLong;/*** 计数器*/ class Counter {private static AtomicLong count new AtomicLong(0);public static long getCount() {return count.get();}public static void incrementCount() {count.incrementAndGet();} }public class Demo {public static void main(String[] args) throws InterruptedException {long count Counter.getCount();System.out.println(count);// 0ListThread list new ArrayList();// 启动10000个线程同时访问计数器for (int i 0; i 10000; i) {Thread thread new Thread(new Runnable() {Overridepublic void run() {try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Counter.incrementCount();}});list.add(thread);}for (Thread thread : list) {thread.start();}for (Thread thread : list) {thread.join();}count Counter.getCount();System.out.println(count);} } 执行结果 第一次10000 第二次10000 第三次10000参考 使用Atomic-廖雪峰的官方网站CAS锁机制无锁、自旋锁、乐观锁、轻量级锁java中的Atomic类
http://www.hkea.cn/news/14467520/

相关文章:

  • 贵阳网站建设设计邱县做网站
  • asp网站变慢网页制作及网站设计
  • 福州网站建设方案深圳装饰装修公司
  • html 网站开发软件东南亚cod建站工具
  • 五合一自助建站网站wordpress code标签
  • wordpress电影网站模板wordpress用户前端登陆地址
  • 毕业设计做网站还是系统温州市建设小学网站首页
  • c mvc 大型网站开发电子商务公司有哪些
  • 建设网站本地调试中国钓鱼网站大全
  • php网站做代理我要发布信息
  • 省机关事务局网站建设管理情况濮阳中强网站建设
  • 音乐网站开发答辩ppt地推网推平台
  • 织梦网站后台管理自己做企业网站的步骤
  • 成都高端模板建站网站架构企业收费标准
  • 自己做效果图的网站水果网页制作模板
  • 网站服务器租金手机端steam怎么调中文
  • 上海专业高端网站建设服务器广西百度seo
  • 重庆做网站 外包公司有哪些网站定制兴田德润实力强
  • 小程序跳转到网站内贸网站有多少
  • 苏州哪家做网站便宜网站如何为关键词做外链
  • 专业的高端网站制作公司淮北公司做网站
  • app手机网站开发什么网站可以免费做会计初级
  • 一个网站需要怎么做北京h5网站制作
  • html个人网站完整代码网络营销外包公司上班
  • 荆州企业网站建设做ar的网站
  • 个人网站收款接口win10有网但是浏览器网页打不开
  • 郑州龙华小学网站建设营销网站建设教学
  • 建网站用哪个好国内知名软件开发公司
  • 接网站建设的单子设计师培训学费
  • 学院网站建设申请报告装潢设计培训