Java Atomic類及線程同步新機制原理解析
一、為什么要使用Atomic類?
看一下下面這個小程序,模擬計數,創建10個線程,共同訪問這個int count = 0 ;每個線程給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() {@Overridepublic 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:2096synchronized:100000000-----time:5765LongAdder:100000000-----time:515
從以上的結果來看并發量達到一定程度運行效率:LongAdder > AtomicLong > synchronized; 這個還只是一個粗略的測試,具體使用還要根據實際情況。
為什么AtomicLong的效率比synchronized的效率高?
AtomicLong的底層使用的是CAS操作(無鎖優化),而synchronized雖然底層做了優化但是并發量達到一定層度,存在鎖的膨脹,最終會變成重量級鎖,需要向操作系統申請鎖資源,所以synchronized的效率慢一點合情合理。
為什么LongAdder的效率比AtomicLong的效率高?
因為LongAdder使用了分段鎖的概念,效率比AtomicLong的效率高。
分段鎖的意思就是用一個數組把線程分成若干組,然后運行結束后把結果累加起來,例如你有1000個線程,數組的長度為4,那就把0-250個放到數組的第0位,以此類推,然后把四個數組中線程的計算結果累加,這樣會很大程度上節省時間,從而提高效率。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
