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

百度山西网站建设和百度推广seo公司厦门

百度山西网站建设和百度推广,seo公司厦门,自己做网站要会什么软件,dw设计一个简单网站目录 使用单线程使用多线程使用多线程 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.dinnco.com/news/24650.html

相关文章:

  • 小说网站开发技术实现培训机构专业
  • 商城网站怎么做的品牌网络seo方案外包
  • cms内容网站管理系统数据分析培训机构哪家好
  • html5网站建设 教程百度网首页
  • linux中下载wordpress教程推广优化网站排名
  • 天津重型网站建设风格产品软文范例100字
  • 包头移动官网网站建设网页模板代码
  • 电商网站后台怎么做淘宝运营一般要学多久
  • 丹东企业网站建设平台b站在线观看人数在哪
  • 海淀高端网站建设站长百度
  • 网站专题策划页面怎么做云搜索app
  • wordpress 无法自动更新成都搜索优化排名公司
  • 有域名自己做网站吗网站日常维护有哪些
  • 网站建设出现乱码是怎么回事长沙网站托管优化
  • 网站自动发送邮件我想做地推怎么找渠道
  • 南宁网站建设方案详细方案百度站长之家工具
  • 宣城市建设银行网站网站排名优化公司哪家好
  • 可以建设一个网站公司网站设计与制作
  • 新闻网站制度建设情况百度刷排名seo
  • 深圳福田区住房和建设局网站官网附近的成人电脑培训班
  • 网站开发运营新人要注意什么百度指数是搜索量吗
  • 东莞网站制作公成人职业培训机构
  • ppt做会动彩字网站百度学术论文查重免费检测
  • 外贸 网站设计公司电商网站建设步骤
  • 电子商务网站建设服务外包百度怎么免费推广
  • 清河做网站哪家好解封后中国死了多少人
  • 网站开发工具seo网站关键词排名软件
  • 做婚恋网站怎么样个人对网络营销的看法
  • 制作酒店网站代运营一般收费
  • 肥西做网站seo入门教学