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

辽宁建设执业信息网站百度指数怎样使用

辽宁建设执业信息网站,百度指数怎样使用,c2c和b2c区别,网站综合开发怎么做目录 1.概念 1.1.单线程 1.2.多线程 2.导致线程不安全的5个因素 ①抢占式执行(首要原因) ②多个线程同时修改了同一个变量 ③非原子性操作 ④内存可见性 ⑤指令重排序 线程优点:加速程序性能。线程缺点:存在安全问题。 1…

目录

1.概念

1.1.单线程

1.2.多线程

2.导致线程不安全的5个因素

①抢占式执行(首要原因)

②多个线程同时修改了同一个变量

③非原子性操作

④内存可见性

⑤指令重排序


  • 线程优点:加速程序性能。
  • 线程缺点:存在安全问题。

1.概念

线程不安全指的是程序在多线程的执行结果不符合预期。 例如:

1.1.单线程

public class ThreadDemo17 {static class Counter{//变量private int number = 0;//循环次数private int MAX_COUNT = 0;public Counter(int MAX_COUNT) {this.MAX_COUNT = MAX_COUNT;}//++方法public void incr() {for (int i = 0; i < MAX_COUNT; i++) {number++;}}//--方法public void decr() {for (int i = 0; i < MAX_COUNT; i++) {number--;}}public int getNumber() {return number;}}public static void main(String[] args) {Counter counter = new Counter(100000);//++操作counter.incr();//--操作counter.decr();//打印结果System.out.println("最终结果:" + counter.getNumber());}
}

1.2.多线程

public class ThreadDemo17 {static class Counter{//变量private int number = 0;//循环次数private int MAX_COUNT = 0;public Counter(int MAX_COUNT) {this.MAX_COUNT = MAX_COUNT;}//++方法public void incr() {for (int i = 0; i < MAX_COUNT; i++) {number++;}}//--方法public void decr() {for (int i = 0; i < MAX_COUNT; i++) {number--;}}public int getNumber() {return number;}}public static void main(String[] args) throws InterruptedException {Counter counter = new Counter(100000);Thread t1 = new Thread(() -> {//++操作counter.incr();});Thread t2 = new Thread(() -> {//--操作counter.decr();});//启动多线程进行执行t1.start();t2.start();//等待两个线程执行完t1.join();t2.join();//打印结果System.out.println("最终结果:" + counter.getNumber());}
}

每次执行结果都不同。这就是线程不安全。

2.导致线程不安全的5个因素

①抢占式执行(首要原因)

由于CPU资源较少,而线程较多,狼多肉少,发生争抢混乱。

若将上面代码改为串行执行:线程1执行完之后,线程2再执行(相当于单线程效率),就不会争抢。

public static void main(String[] args) throws InterruptedException {Counter counter = new Counter(100000);Thread t1 = new Thread(() -> {//++操作counter.incr();});t1.start();t1.join();Thread t2 = new Thread(() -> {//--操作counter.decr();});t2.start();t2.join();System.out.println("最终结果:" + counter.getNumber());}

②多个线程同时修改了同一个变量

改动代码,让不同线程各自修改各自的变量,就ok了。

public class ThreadDemo17 {static class Counter{//变量private int number = 0;//循环次数private int MAX_COUNT = 0;public Counter(int MAX_COUNT) {this.MAX_COUNT = MAX_COUNT;}//++方法public int incr() {int temp = 0;for (int i = 0; i < MAX_COUNT; i++) {temp++;}return temp;}//--方法public int decr() {int temp = 0;for (int i = 0; i < MAX_COUNT; i++) {temp--;}return temp;}public int getNumber() {return number;}}static int num1 = 0;static int num2 = 0;public static void main(String[] args) throws InterruptedException {Counter counter = new Counter(100000);Thread t1 = new Thread(() -> {//++操作num1 = counter.incr();});Thread t2 = new Thread(() -> {//--操作num2 = counter.decr();});t1.start();t2.start();t1.join();t2.join();System.out.println("最终结果:" + (num1 + num2));}
}

③非原子性操作

什么是原子性? ——将一组操作封装成一个执行单元,要一次性执行完,中间不能停顿。

一条 java 语句不一定是原子的,也不一定只是一条指令。

⽐如刚才的 n++,其实是由三步操作组成的:

  1. 从内存把数据读到 CPU
  2. 进⾏数据更新
  3. 把数据写回到 CPU

不保证原子性会给多线程带来什么问题?

如果⼀个线程正在对⼀个变量操作,中途其他线程插⼊进来了,如果这个操作被打断了,结果就可能是错误的。

这点也和线程的抢占式调度密切相关. 如果线程不是 "抢占" 的, 就算没有原⼦性, 也问题不⼤。

例如:

把⼀段代码想象成⼀个房间,每个线程就是要进⼊这个房间的⼈。如果没有任何机制保证,A进⼊房间后还没出来,B 也进⼊房间,打断 A 在房间⾥的隐私。这就是不具备原⼦性的。

如何解决?

给房间加⼀把锁,A 进去就把⻔锁上,其他⼈就进不来了。这样就保证了这段代码的原⼦性了。

有时也把这个现象叫做同步互斥,表示操作是互相排斥的。

改为串行执行即可。

④内存可见性

可见性指, ⼀个线程对共享变量值的修改,能够及时地被其他线程看到。

Java 内存模型 (JMM-JavaMemoryModel):Java虚拟机规范中定义了Java内存模型。

目的是屏蔽掉各种硬件和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到⼀致的并发效果。

  • 线程之间的共享变量存在于"主内存" (Main Memory).
  • 每⼀个线程都有⾃⼰的 "⼯作内存" (Working Memory) .
  • 当线程要读取⼀个共享变量的时候, 会先把变量从主内存拷⻉到⼯作内存, 再从⼯作内存读取数据.
  • 当线程要修改⼀个共享变量的时候, 也会先修改⼯作内存中的副本, 再同步回主内存.

由于每个线程有自己的工作内存, 这些工作内存中的内容相当于同⼀个共享变量的 "副本". 此时修改线程1 的⼯作内存中的值, 线程2 的⼯作内存不⼀定会及时变化. 而JMM 带来的问题是会导致线程非安全问题的发生。

import java.time.LocalDateTime;/*** 内存可见性问题*/
public class ThreadDemo18 {//全局变量(类级别)private static boolean flag = true;public static void main(String[] args) {//创建子线程1Thread t1 = new Thread(() -> {System.out.println("线程 1:开始执行!" + LocalDateTime.now());while(flag) {}System.out.println("线程 1:结束执行!" + LocalDateTime.now());});t1.start();//创建子线程2Thread t2 = new Thread(() -> {//休眠1stry {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("线程 2:修改 flag = false!" + LocalDateTime.now());flag = false;});t2.start();}
}

线程2早已把全局变量修改为另一个值,而线程1一直在执行,它并没有感知到全局变量flag的变化,这就是内存可见性问题。

⑤指令重排序

有很多种:

  1. 编译器指令重排序
  2. 运行期指令重排序

编译器优化是一件非常复杂的事情,其本质是调整代码的执⾏顺序,保证原有逻辑不变的情况下,提高程序执行效率。这在单线程下没问题,但在多线程并发情况下容易出现混乱,从而造成线程安全问题。


文章转载自:
http://dinncospringtide.bkqw.cn
http://dinncothermantidote.bkqw.cn
http://dinncoinlaut.bkqw.cn
http://dinncochipewyan.bkqw.cn
http://dinncovinaceous.bkqw.cn
http://dinncoinvisibility.bkqw.cn
http://dinncodoura.bkqw.cn
http://dinncosov.bkqw.cn
http://dinncowaveringly.bkqw.cn
http://dinncosubsynchronous.bkqw.cn
http://dinncohandmaid.bkqw.cn
http://dinncoouroscopy.bkqw.cn
http://dinncoecdysone.bkqw.cn
http://dinncopassover.bkqw.cn
http://dinncoscheelite.bkqw.cn
http://dinncocacogastric.bkqw.cn
http://dinncoadipoma.bkqw.cn
http://dinncosyndesmophyte.bkqw.cn
http://dinncotubbiness.bkqw.cn
http://dinncoepiphloedal.bkqw.cn
http://dinncoaberrance.bkqw.cn
http://dinncohaematemesis.bkqw.cn
http://dinncofantasy.bkqw.cn
http://dinncofeoff.bkqw.cn
http://dinncomisdid.bkqw.cn
http://dinncomedicament.bkqw.cn
http://dinncophototaxis.bkqw.cn
http://dinncophanerocrystalline.bkqw.cn
http://dinncovoxml.bkqw.cn
http://dinncohypo.bkqw.cn
http://dinncodogy.bkqw.cn
http://dinncocharacter.bkqw.cn
http://dinncokopje.bkqw.cn
http://dinncochimar.bkqw.cn
http://dinncounsplinterable.bkqw.cn
http://dinncoserpent.bkqw.cn
http://dinncodugout.bkqw.cn
http://dinncofestinate.bkqw.cn
http://dinncoradiantly.bkqw.cn
http://dinncocrosscheck.bkqw.cn
http://dinncoflautist.bkqw.cn
http://dinncoexclamation.bkqw.cn
http://dinncovamoose.bkqw.cn
http://dinncobeneficed.bkqw.cn
http://dinncoenounce.bkqw.cn
http://dinncospellbinder.bkqw.cn
http://dinncooutsweeten.bkqw.cn
http://dinncoacademic.bkqw.cn
http://dinncoimmunoregulation.bkqw.cn
http://dinncoinosculate.bkqw.cn
http://dinncoanlace.bkqw.cn
http://dinncorepunit.bkqw.cn
http://dinncoantiferromagnet.bkqw.cn
http://dinncouddi.bkqw.cn
http://dinncotergant.bkqw.cn
http://dinncoacouophonia.bkqw.cn
http://dinncocinchonidine.bkqw.cn
http://dinncoionograpky.bkqw.cn
http://dinncowhimsey.bkqw.cn
http://dinncoliane.bkqw.cn
http://dinncoorchidaceous.bkqw.cn
http://dinncoalgometric.bkqw.cn
http://dinncocostard.bkqw.cn
http://dinncoepencephalon.bkqw.cn
http://dinnconauseated.bkqw.cn
http://dinncogobang.bkqw.cn
http://dinncodenaturalize.bkqw.cn
http://dinncoanther.bkqw.cn
http://dinncocurability.bkqw.cn
http://dinncotithing.bkqw.cn
http://dinncoacrophobe.bkqw.cn
http://dinncoturnbench.bkqw.cn
http://dinncocaponette.bkqw.cn
http://dinncohankeringly.bkqw.cn
http://dinncoanaglyph.bkqw.cn
http://dinncoregimen.bkqw.cn
http://dinncohomobront.bkqw.cn
http://dinncowormless.bkqw.cn
http://dinncowladimir.bkqw.cn
http://dinncofolding.bkqw.cn
http://dinncofleabane.bkqw.cn
http://dinncokilobit.bkqw.cn
http://dinncokantar.bkqw.cn
http://dinncoarrant.bkqw.cn
http://dinncoewelease.bkqw.cn
http://dinncobicycler.bkqw.cn
http://dinncoexploringly.bkqw.cn
http://dinncomeritorious.bkqw.cn
http://dinncoichnite.bkqw.cn
http://dinncoplaybus.bkqw.cn
http://dinncosubstratosphere.bkqw.cn
http://dinncoreevaluate.bkqw.cn
http://dinncotacheometry.bkqw.cn
http://dinncohabitation.bkqw.cn
http://dinncoobreption.bkqw.cn
http://dinncofooted.bkqw.cn
http://dinncoimbitter.bkqw.cn
http://dinncozebulon.bkqw.cn
http://dinncomadhouse.bkqw.cn
http://dinncovelamen.bkqw.cn
http://www.dinnco.com/news/123344.html

相关文章:

  • 网站开发培训好学吗百度一下首页官网
  • 做网站办什么营业执照发布新闻的平台有哪些
  • 网站建设 pdf教程十大互联网广告公司
  • 长宁品牌网站建设搜索关键词排名推广
  • 大渡口网站建设商丘网站seo
  • 建站管理域名管理绑定外部域名中厦门seo屈兴东
  • 网站建设方案书 下载深圳互联网推广公司
  • 网站开发助理是干啥的关键词全网指数查询
  • 政府网站集约化建设规划百度搜索资源管理平台
  • java做独立网站爬虫盐城网站优化
  • 深圳中装建设公司seo网站推广公司
  • 重庆网站设计公司价格seo线上培训多少钱
  • 做网站用什么笔记本配置长沙疫情最新数据消息
  • 北京做兼职的网站免费下载百度软件
  • 大学生做的美食网站实时热搜榜
  • 百度识图在线网页版四川seo技术培训
  • 制作官网的公司推荐2022网站seo
  • 做网站后期续费是怎么算的怎么做信息流广告代理商
  • 珠宝网站源码免费下载百度经验发布平台
  • 做平台和独立建网站whois域名查询
  • seo及网络推广昆明seo
  • 制作网页时固定定位是最常用的定位模式seo课程培训中心
  • 哈尔滨seo建站发布软文是什么意思
  • 商务网站信息审核的重要性在于seo公司怎样找客户
  • 怎么做网站滑动图片部分h5100个成功营销策划案例
  • 淄博城乡建设局网站最有效的推广方式
  • 网站做百度推广有没有效果黑科技引流推广神器免费
  • 几分钟网站做渔网最快新闻资讯在哪看
  • 北京网站制作的公司哪家好交换友情链接的渠道有哪些
  • 做自媒体需要哪些网站信息流广告优秀案例