多线程下实现自增的几种方式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                多线程下实现自增的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                前情回顧:i++操作并不是原子操作,因此多線程下會達不到預期的效果,需要通過加鎖或AtomicInteger或LongAdder等方法來實現。
i++可以分為三步
我們通過實驗來觀察實現i++操作的方式。
下面實驗中通過繼承Thread實現了多線程
?
錯誤方法:
1.多線程下直接進行自增操作
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");} }運行結果:
可以看出,與預期的100000差別很大。
2.通過 volatile來實現
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");} }?
運行結果:
volatile也無法達到預期效果,因為volatile只可以實現可見性以及禁止指令重排。
當a為1時,線程1與線程2都取出a,線程1實現了a++的操作但并未將值寫入內存(為寫入內存時其他線程看不到),此時線程2也開始執行a++的操作,線程1開始把a=2寫入內存,線程2開始把a=2寫入內存,
兩個線程執行完之后a的值為2。
正確的實現方式:
1.加鎖synchronized
加鎖的幾種方式:
對當前對象加鎖,對這個類加鎖,對這個方法加鎖,對一個對象加鎖(但不能是int等基礎類型)
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");} }運行結果:
2. 通過AtomicInteger實現
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");} }?
運行結果:
3.LongAdder實現
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");} }運行結果:
?
總結
以上是生活随笔為你收集整理的多线程下实现自增的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【剑指offer】面试题11:旋转数组的
- 下一篇: 7-4 堆栈模拟队列 (25 分)
