日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

多线程下实现自增的几种方式

發(fā)布時(shí)間:2024/7/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程下实现自增的几种方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前情回顧:i++操作并不是原子操作,因此多線程下會(huì)達(dá)不到預(yù)期的效果,需要通過(guò)加鎖或AtomicInteger或LongAdder等方法來(lái)實(shí)現(xiàn)。

i++可以分為三步

我們通過(guò)實(shí)驗(yàn)來(lái)觀察實(shí)現(xiàn)i++操作的方式。

下面實(shí)驗(yàn)中通過(guò)繼承Thread實(shí)現(xiàn)了多線程

?

錯(cuò)誤方法:

1.多線程下直接進(jìn)行自增操作

public class byte1 extends Thread{static int a = 0;@Overridepublic void run() {for(int i=1;i<=10000;i++) {a++;}}public static void main(String[] args) {long nowTimeMillis=System.currentTimeMillis(); byte1 tByte1 = new byte1();List<Thread> threads = new ArrayList<Thread>();for(int i=1;i<=10;i++) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1=System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillis+"ms");} }

運(yùn)行結(jié)果:

可以看出,與預(yù)期的100000差別很大。

2.通過(guò) volatile來(lái)實(shí)現(xiàn)

public class byte1 extends Thread{volatile static int a = 0;@Overridepublic void run() {for(int i=1;i<=10000;i++) {a++;}}public static void main(String[] args) {long nowTimeMillis=System.currentTimeMillis(); byte1 tByte1 = new byte1();List<Thread> threads = new ArrayList<Thread>();for(int i=1;i<=10;i++) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1=System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillis+"ms");} }

?

運(yùn)行結(jié)果:

volatile也無(wú)法達(dá)到預(yù)期效果,因?yàn)関olatile只可以實(shí)現(xiàn)可見性以及禁止指令重排。

當(dāng)a為1時(shí),線程1與線程2都取出a,線程1實(shí)現(xiàn)了a++的操作但并未將值寫入內(nèi)存(為寫入內(nèi)存時(shí)其他線程看不到),此時(shí)線程2也開始執(zhí)行a++的操作,線程1開始把a(bǔ)=2寫入內(nèi)存,線程2開始把a(bǔ)=2寫入內(nèi)存,

兩個(gè)線程執(zhí)行完之后a的值為2。

正確的實(shí)現(xiàn)方式:

1.加鎖synchronized

加鎖的幾種方式:

對(duì)當(dāng)前對(duì)象加鎖,對(duì)這個(gè)類加鎖,對(duì)這個(gè)方法加鎖,對(duì)一個(gè)對(duì)象加鎖(但不能是int等基礎(chǔ)類型)

synchronized(this){do(); }synchronized(T.class){do(); }synchronized m(){do(); }Object o = new Object(); synchronized(o){do(); } public class byte1 extends Thread{static int a = 0;@Overridepublic void run() {synchronized (this) {for(int i=1;i<=10000;i++) {a++;}}}public static void main(String[] args) {long nowTimeMillis=System.currentTimeMillis(); byte1 tByte1 = new byte1();List<Thread> threads = new ArrayList<Thread>();for(int i=1;i<=10;i++) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1=System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillis+"ms");} }

運(yùn)行結(jié)果:

2. 通過(guò)AtomicInteger實(shí)現(xiàn)

public class byte1 extends Thread{//static int a = 0;static AtomicInteger a = new AtomicInteger(0);@Overridepublic void run() {synchronized (this) {for(int i=1;i<=10000;i++) {a.incrementAndGet();}}}public static void main(String[] args) {long nowTimeMillis=System.currentTimeMillis(); byte1 tByte1 = new byte1();List<Thread> threads = new ArrayList<Thread>();for(int i=1;i<=10;i++) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a.get());long nowTimeMillis1=System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillis+"ms");} }

?

運(yùn)行結(jié)果:

3.LongAdder實(shí)現(xiàn)

public class byte1 extends Thread{//static int a = 0;static LongAdder a = new LongAdder();@Overridepublic void run() {for(int i=1;i<=10000;i++) {a.increment();}}public static void main(String[] args) {long nowTimeMillis=System.currentTimeMillis(); byte1 tByte1 = new byte1();List<Thread> threads = new ArrayList<Thread>();for(int i=1;i<=10;i++) {threads.add(new Thread(tByte1));}for(Thread thread :threads) {thread.start();}for(Thread thread :threads) {try {thread.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(a);long nowTimeMillis1=System.currentTimeMillis();System.out.println(nowTimeMillis1-nowTimeMillis+"ms");} }

運(yùn)行結(jié)果:

?

總結(jié)

以上是生活随笔為你收集整理的多线程下实现自增的几种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 1000部国产精品成人观看 | 一级伦理农村妇女愉情 | 可以看的毛片 | 国产九区 | 国产精品老牛影视 | 国产三级第一页 | 男人的天堂视频在线观看 | 成人污污www网站免费丝瓜 | 黄色片免费在线观看 | 精品人妻一区二区三区四区不卡 | 尤物国产在线 | 午夜丁香婷婷 | 日本网站免费观看 | 国产日本在线 | 中文字幕视频免费观看 | av在线资源网 | av一道本| 尤物精品在线观看 | 国产一级自拍 | 国产伦精品一区二区三区视频孕妇 | 日本三级吹潮 | 欧美性猛交ⅹxx乱大交 | 91丝袜呻吟高潮美腿白嫩在线观看 | 天堂а√在线中文在线 | 日韩裸体视频 | 国产精品一区二区黑人巨大 | 晨勃顶到尿h1v1 | 国产亚洲精品av | 天天干天天玩 | 你懂的视频网站 | 最新在线视频 | 可以看毛片的网站 | 浓精喷进老师黑色丝袜在线观看 | 精品视频导航 | 欧美不卡在线视频 | 痴女扩张宫交脱垂重口小说 | 狠狠干2024| 国产又粗又猛又爽又黄又 | 成都4电影免费高清 | 日日夜夜精品 | 97人人澡人人爽人人模亚洲 | 亚洲欧美视频二区 | 精品少妇theporn| 99精品国产成人一区二区 | 国产一区二区三区久久 | yw在线观看 | 久久天天躁狠狠躁夜夜av | 亚洲精品91 | 看片在线观看 | 国产小视频免费在线观看 | 成人在线亚洲 | av免费在线观看不卡 | 国产丝袜一区二区三区 | 三级免费观看 | 国产精品亚洲专区无码牛牛 | 春草| 国产农村老头老太视频 | 熊出没之冬日乐翻天免费高清观看 | sm调教羞耻姿势图片 | 久久六 | 嫩草伊人久久精品少妇av | www.爱色av.com | 国内视频一区二区三区 | 午夜爽爽视频 | 国产亚洲精品久久久久久777 | 成人一区三区 | 好吊操妞 | 午夜影视福利 | 九九九国产| 男人天堂av电影 | 亚洲综合不卡 | 四虎精品 | 蜜桃传媒一区二区亚洲 | 在线观看亚洲专区 | 日韩一区二区三区精品视频 | 国产99久久久久 | 国产欧美一区二区三区视频 | 国产精品久久久久久亚洲色 | 一本色道久久88加勒比—综合 | 国产一区二区小说 | 国产乱淫av一区二区三区 | 欧美性生话 | 国产不卡av在线 | 青青草国产一区二区三区 | 久久人妻少妇嫩草av无码专区 | 国产精品毛片一区二区三区 | 久久久久久久久久久久久久 | 波多野结衣理论片 | 中文字幕亚洲图片 | 精品亚洲成人 | 麻豆黄色网址 | 午夜视频色 | www.99视频| a级片中文字幕 | 欧美乱三级 | 日韩六十路 | 国产a一级| 亚洲精品成人电影 | 国产一区二区视频在线免费观看 |