Atomic類和執行緒同步新機制
一、為什麼要使用Atomic類?
看一下下面這個小程式,模擬計數,建立10個執行緒,共同訪問這個int count = 0 ;,huichuux每個執行緒給count往上加10000,這個時候你需要加鎖,如果不加鎖會出現執行緒安全問題,但是使用AtomicInteger之後就不用再做加鎖的操作了,因為AtomicInteger內部使用了CAS操作,直接無鎖往上遞增,有人會問問什麼會出現無鎖操作,答案只有一個:那就是快唄;
下面是AtomicInteger的使用方法:
package com.example.demo.threaddemo.juc_008; import java.util.ArrayList;import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** * @author D-L * @Classname T01_AtomicInteger * @Version 1.0 * @Description 使用AtomicInteger類代替synchronized * @Date 2020/7/22 */ public class T01_AtomicInteger { // int count = 0; AtomicInteger count = new AtomicInteger(0);public /**synchronized*/ void m(){ for (int i = 0; i < 10000; i++) { // count++; count.incrementAndGet(); } } public static void main(String[] args) { T01_AtomicInteger t = new T01_AtomicInteger(); List<Thread> threads = new ArrayList<>();for (int i = 0; i < 10; i++) { threads.add(new Thread(t::m ,"Thread" + i)); } threads.forEach(o -> o.start()); threads.forEach(o ->{ try { o.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); /* for (int i = 0; i < 10; i++) { new Thread(t::m ,"Thread"+i).start(); } try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }*/ System.out.println(t.count); } }
二、Atomic類,synchronized、LongAdder的效率驗證 及 分析
模擬多個執行緒對一個數進行遞增,多執行緒對一個共享變數進行遞增的方法大概有三種;驗證一下它們的效率,這裡做一些粗糙的測試,基本已經能說明問題,具體情況還要根據實際情況:
- 第一種:使用long count = 0; 加鎖來實現;
- 第二種:使用AtomicLong類來實現;
- 第三種:使用LongAdder實現;
package com.example.demo.threaddemo.juc_008; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAdder; /** * @author D-L * @Classname T02_AtomicVsSyncVsLongAdder * @Version 1.0 * @Description 測試Atomic類 synchronized LongAdder效率 * @Date 2020/7/22 */ public class T02_AtomicVsSyncVsLongAdder { static AtomicLong count1 = new AtomicLong(0L); static Long count2 = 0L; static LongAdder count3 = new LongAdder(); public static void main(String[] args) throws InterruptedException { Thread [] threads = new Thread[1000]; /*-----------------------------------Atomic類-----------------------------------*/ for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() ->{ for (int j = 0; j < 100000; j++) { count1.incrementAndGet(); } }); } long start = System.currentTimeMillis(); for (Thread t : threads) t.start(); for (Thread t : threads) t.join(); long end = System.currentTimeMillis(); System.out.println("Atomic:" + count1.get() +"-----time:" +(end - start)); /*----------------------------------synchronized---------------------------------*/ Object lock = new Object(); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(new Runnable() { @Override public void run() { for (int j = 0; j < 100000; j++) { synchronized (lock) { count2++; } } } }); } long start2 = System.currentTimeMillis(); for (Thread t : threads) t.start(); for (Thread t : threads) t.join(); long end2 = System.currentTimeMillis(); System.out.println("synchronized:" + count1.get() +"-----time:" +(end2 - start2)); /*-------------------------------------LongAdder----------------------------------*/ for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(() ->{ for (int j = 0; j < 100000; j++) { count3.increment(); } }); } long start3 = System.currentTimeMillis(); for (Thread t : threads) t.start(); for (Thread t : threads) t.join(); long end3 = System.currentTimeMillis(); System.out.println("LongAdder:" + count1.get() +"-----time:" +(end3 - start3)); } }
/*----------------------------------執行結果---------------------------------*/
Atomic:100000000-----time:2096
synchronized:100000000-----time:5765
LongAdder:100000000-----time:515
從以上的結果來看併發量達到一定程度執行效率:LongAdder > AtomicLong > synchronized; 這個還只是一個粗略的測試,具體使用還要根據實際情況。
- 為什麼AtomicLong的效率比synchronized的效率高?
AtomicLong的底層使用的是CAS操作(無鎖優化),而synchronized雖然底層做了優化但是併發量達到一定層度,存在鎖的膨脹,最終會變成重量級鎖,需要向作業系統申請鎖資源,所以synchronized的效率慢一點合情合理。
- 為什麼LongAdder的效率比AtomicLong的效率高?
因為LongAdder使用了分段鎖的概念,效率比AtomicLong的效率高。
分段鎖的意思就是用一個數組把執行緒分成若干組,然後執行結束後把結果累加起來,例如你有1000個執行緒,陣列的長度為4,那就把0-250個放到陣列的第0位,以此類推,然後把四個陣列中執行緒的計算結果累加,這樣會很大程度上節省時間,從而提高效率。