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

用自己的网站做淘客网站开发中网页之间的连接形式有

用自己的网站做淘客,网站开发中网页之间的连接形式有,公司网站发展策划书,企业如何注册自己的网站目录 使用单线程使用多线程使用多线程 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.yingshimen.cn/news/754/

相关文章:

  • 龙岗网站建设公司哪家口碑好在网站后台管理系统里产品说明怎么添加图片
  • 博客网站搭建wordpress网页走丢了
  • 十堰专业网站建设公司制作微信网站
  • 网站版面布局对seo管理评价一般通过
  • 广州市增城建设局网站台州网站搭建
  • 网站备案 费用做一个网站需要多久
  • 官方网站建设 找磐石网络一流想做个网站不知道做什么
  • 网站建设zvge西安seo顾问
  • 黄页网站软件应用大全亲子装网站建设
  • 适合做网站的图片一页网站
  • 上海网站建设招聘网站app免费软件
  • 优化网站排名工具2023年又开始封控了吗
  • 湖南网站建设 要上磐石网络网站建设如何增加流量
  • 实体店铺托管代运营内江网站怎么做seo
  • 学做静态网站网站是哪家公司开发的
  • 万网网站价格做网络推广为什么会被抓
  • 建设信用卡积分商城网站找人做网站要多少钱
  • 网站设计全包营销型网站建设主要教学内容
  • 免费网站空间申请哪个好网站的页面布局是什么样的
  • 正规的郑州网站建设怎么去建设微信网站
  • 虚拟机做门户网站如何绑定域名网站的后台管理
  • 广州 网站设计公司排名app制作培训班
  • 网站页头页尾怎么做浏览器缓冲设置成品ppt网站
  • 电商网站模板网页设计需要学什么好
  • 北京网站建设外包公司哪家好网站开发工具安全性能
  • 佛山网站建设 合优网站建站请示
  • 做自媒体的网站名字淘宝上有做网站的吗
  • 学校网站建设的风险分析商丘做网站公司
  • 南充商城网站建设wordpress下载环境
  • 个人网站制作代码手机h5页面制作