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

中国互联网协会属于什么单位seo公司排行

中国互联网协会属于什么单位,seo公司排行,做劳保批发的网站,如何创建平台类网站目录 使用单线程使用多线程使用多线程 synchronized使用多线程 原子类AtomicLong 使用单线程 单线程修改计数器的值,没有发生问题,每次运行结果都是10000,不过程序耗时较长 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);// 0List<Thread> 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);// 0List<Thread> 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

原理是CAS(Compare 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);// 0List<Thread> 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

参考

  1. 使用Atomic-廖雪峰的官方网站
  2. CAS锁机制(无锁、自旋锁、乐观锁、轻量级锁)
  3. java中的Atomic类
http://www.hengruixuexiao.com/news/34524.html

相关文章:

  • 网站制作培训课程世界足球排名最新
  • 做婚纱摄影网站多少钱seo技术培训沈阳
  • 电子商务网站建设的特点seo优化培训
  • 北京免费做网站谷歌搜索引擎入口2021
  • 网站建设总体目标网络营销论文5000字
  • 怎么学做一件完整衣服网站优化网站结构一般包括
  • 建外贸网站 东莞seo教学免费课程霸屏
  • 开源独立站新闻今天最新消息
  • b站大全不收费优化seo培训班
  • 公司内部网站管理系统百度快照的作用是什么
  • 网页设计中用div做网站例子国际新闻最新消息今天
  • 如何做网站镜像广告策划
  • 洪梅镇仿做网站新手做外贸怎么入门
  • 哪些网站做婚纱摄影上海培训机构排名榜
  • wordpress 外观 自定义 没反应关键词排名优化是什么意思
  • 上海网站建设|网站制作seo网站推广是什么意思
  • 网络存储上做网站168推广网
  • 网站设计制作太原刚刚北京传来重大消息
  • 网络推广费用seo怎么才能做好
  • 简约的网站设计免费外链发布
  • 我网站关键词太多网络小说排行榜
  • 龙岩市永定区疫情最新消息茂名seo快速排名外包
  • 网站开发学习百度搜索网
  • 携程旅游网站建设的定位推广计划方案
  • 浚县网站建设电商平台app大全
  • 高青网站建设东营seo整站优化
  • 陈塘庄做网站公司有哪些免费网站可以发布广告
  • html5网站设计网站优化公司大家好
  • 嘉兴seo网站排名优化百度网站优化培训
  • 一般网站建设步骤秘密入口3秒自动进入