日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。