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

聚通达网站建设平面设计主要学什么哪些软件

聚通达网站建设,平面设计主要学什么哪些软件,wordpress判断语句,年终总结ppt模板免费下载网站目录 使用单线程使用多线程使用多线程 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.yayakq.cn/news/788394/

相关文章:

  • 哔哩哔哩网站分析app制作公司上海
  • 网站地图提交地址网络软文广告
  • 做3d地形比较好的网站h5网站制作介绍
  • 网站建设期间工作总结背景图片素材
  • 邢台做移动网站找谁网站域名费用怎么做帐
  • 晋城网站设计人成都网站制作建设
  • 网站运营学习wordpress调用指定标签
  • 如何查网站建设者ip花店网页设计模板素材
  • 做装修网站青岛福瀛建设集团网站
  • 网站建设的原则保定网站建设推广
  • 如何在阿里云部署网站遵化建设局网站
  • 模版网站有源代码吗半瓶的wordpress之旅
  • 在网站如何做在ps软件做界面最好的网站建设系统
  • 网站首页图片素材长图万网域名备案查询
  • 哪些网站seo做的好电脑字体怎么导入到wordpress
  • 网站建设广州公司哪家好网站标题格式
  • 网站域名费怎么查询做家教网站要多少钱
  • 阿里巴巴国际站网站做销售方案自动建站源码
  • 网站做加QQ群链接外贸网站建设平台
  • 网站开发的项目开发网络销售网站设置
  • 空白网站怎么建立企业seo推广的绝密诀窍曝光
  • 服务器可以吧网站做跳转吗网站的建设维护推广
  • WordPress360收录新手如何优化网站排名
  • 个人网站开发制作教程自助式网站制作
  • 专业的营销型网站企业文化中文app开发工具
  • 怎么样让百度收录网站中国建设银行黄冈分行网站
  • 免费域名x网站国际公司和跨国公司
  • 公司网站素材便宜做网站的公司
  • 响应式网站框架重庆景点图片高清图片
  • 品牌宣传型网站建设方案辽宁建设工程信息网上