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

网站建设从入门湖北百度推广电话

网站建设从入门,湖北百度推广电话,wordpress阿里矢量图使用方法,东莞学校网站建设目录 使用单线程使用多线程使用多线程 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.15wanjia.com/news/6801.html

相关文章:

  • 小说网站开发技术实现今天头条新闻100条
  • 百度网盘资源搜索入口简述搜索引擎优化的方法
  • 西樵网站制作公司青岛推广优化
  • 目前有哪些网络营销方式提升seo排名的方法
  • 网站日程建设表国际热点事件
  • 响应式网站建设如何网站首页布局设计模板
  • 成都j网站制作苏州seo优化公司
  • html css js手机 移动 网站 分享连接 一键分享品牌广告文案
  • 智慧政务网站怎么做媒体发稿费用
  • 网站网页设计优秀案例阿里云官网首页
  • 做全景图二维码的网站网页制作步骤
  • wordpress连接WEB网页网站seo优化公司
  • 现在流行的网站开发制作工具站长工具权重
  • 网页游戏大全小游戏珠海关键词优化软件
  • 高埗镇仿做网站网络培训系统
  • 宝宝投票网站怎么做刷网站seo排名软件
  • 乌克兰设计网站建设seo排名官网
  • 微网站建设高端网站定制武汉seo结算
  • 天津建设工程竣工备案公示网站五年级上册语文优化设计答案
  • 做网站有哪些行业google谷歌搜索引擎入口
  • 做网站买域名怎么在百度上发布自己的信息
  • 携程旅游网站建设的定位搜索引擎优化哪些方面
  • 电商数据seo规范培训
  • 浚县网站建设免费发广告的网站大全
  • 外贸网站排名seo站长优化工具
  • 国外建站 网站 推荐淘宝关键词排名查询工具免费
  • 有没有什么网站做卷子2020做seo还有出路吗
  • 蜂网站开发iis7站长工具
  • 苏州推广网站建设概况seo门户网站建设方案
  • 那种漂亮的网站怎么做的网络营销推广方案论文