多线程下实现自增的几种方式
前情回顧: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)題。
- 上一篇: 【剑指offer】面试题11:旋转数组的
- 下一篇: 7-4 堆栈模拟队列 (25 分)