您好,登錄后才能下訂單哦!
小編給大家分享一下Java中常用的原子類(lèi)有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Java中提供了一些原子類(lèi),原子類(lèi)包裝了一個(gè)變量,并且提供了一系列對(duì)變量進(jìn)行原子性操作的方法。我們?cè)诙嗑€程環(huán)境下對(duì)這些原子類(lèi)進(jìn)行操作時(shí),不需要加鎖,大大簡(jiǎn)化了并發(fā)編程的開(kāi)發(fā)。
目前Java中提供的原子類(lèi)大部分底層使用了CAS鎖(CompareAndSet自旋鎖),如AtomicInteger、AtomicLong等;也有使用了分段鎖+CAS鎖的原子類(lèi),如LongAdder等。
AtomicInteger與AtomicLong的底層實(shí)現(xiàn)與用法基本相同,不同點(diǎn)在于AtomicInteger包裝了一個(gè)Integer型變量,而AtomicLong包裝了一個(gè)Long型變量。
AtomicInteger與AtomicLong的底層實(shí)現(xiàn)都使用了CAS鎖。
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; /** * @author IT00ZYQ * @date 2021/5/24 15:33 **/ public class T13_AtomicInteger { private static AtomicInteger atomicInteger = new AtomicInteger(); private static AtomicLong atomicLong = new AtomicLong(); private static Integer integer = 0; private static Long lon = 0L; public static void main(String[] args) { // 創(chuàng)建10個(gè)線程,分別對(duì)atomicInteger、atomicLong、integer、lon進(jìn)行1000次增加1的操作 // 如果操作是原子性的,那么正確結(jié)果 = 10 * 1000 = 10000 Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(() -> { for (int j = 1; j <= 1000; j++) { atomicInteger.incrementAndGet(); atomicLong.incrementAndGet(); integer ++; lon ++; } }); } // 啟動(dòng)線程 for (Thread thread : threads) { thread.start(); } // 保證10個(gè)線程運(yùn)行完成 try { for (Thread thread : threads) { thread.join(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("AtomicInteger的結(jié)果:" + atomicInteger); System.out.println("AtomicLong的結(jié)果:" + atomicLong); System.out.println("Integer的結(jié)果:" + integer); System.out.println("Long的結(jié)果:" + lon); } }
運(yùn)行結(jié)果:
AtomicInteger的結(jié)果:10000
AtomicLong的結(jié)果:10000
Integer的結(jié)果:4880
Long的結(jié)果:4350
Process finished with exit code 0
多次運(yùn)行發(fā)現(xiàn)原子類(lèi)AtomicInteger與AtomicLong每次都能得到正確的結(jié)果10000,但是非原子類(lèi)Integer與Long一般情況下都達(dá)不到10000,每次的結(jié)果也可能不一樣。
LongAdder的底層實(shí)現(xiàn)使用了分段鎖,每個(gè)段使用的鎖是CAS鎖,所以LongAdder的底層實(shí)現(xiàn)是分段鎖+CAS鎖。
在上面的程序添加了一個(gè)LongAdder變量進(jìn)行測(cè)試
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAdder; /** * @author IT00ZYQ * @date 2021/5/24 15:33 **/ public class T13_AtomicInteger { private static AtomicInteger atomicInteger = new AtomicInteger(); private static AtomicLong atomicLong = new AtomicLong(); private static LongAdder longAdder = new LongAdder(); private static Integer integer = 0; private static Long lon = 0L; public static void main(String[] args) { // 創(chuàng)建10個(gè)線程,分別對(duì)atomicInteger、atomicLong、integer、lon進(jìn)行1000次增加1的操作 // 如果操作是原子性的,那么正確結(jié)果 = 10 * 1000 = 10000 Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(() -> { for (int j = 1; j <= 1000; j++) { atomicInteger.incrementAndGet(); atomicLong.incrementAndGet(); integer ++; lon ++; longAdder.increment(); } }); } // 啟動(dòng)線程 for (Thread thread : threads) { thread.start(); } // 保證10個(gè)線程運(yùn)行完成 try { for (Thread thread : threads) { thread.join(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("AtomicInteger的結(jié)果:" + atomicInteger); System.out.println("AtomicLong的結(jié)果:" + atomicLong); System.out.println("Integer的結(jié)果:" + integer); System.out.println("Long的結(jié)果:" + lon); System.out.println("LongAdder的結(jié)果:" + longAdder); } }
運(yùn)行結(jié)果:
AtomicInteger的結(jié)果:10000
AtomicLong的結(jié)果:10000
Integer的結(jié)果:6871
Long的結(jié)果:6518
LongAdder的結(jié)果:10000
Process finished with exit code 0
LongAdder類(lèi)也是能夠正確輸出結(jié)果的。
package juc; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAdder; /** * @author IT00ZYQ * @date 2021/5/24 15:51 **/ public class T14_AtomicClassPerformance { private static AtomicLong atomicLong = new AtomicLong(); private static LongAdder longAdder = new LongAdder(); /** * 線程數(shù) */ private static final int THREAD_COUNT = 100; /** * 每次線程循環(huán)操作次數(shù) */ private static final int OPERATION_COUNT = 10000; public static void main(String[] args) { Thread[] threads = new Thread[THREAD_COUNT]; // 創(chuàng)建對(duì)AtomicLong進(jìn)行操作的線程 for (int i = 0; i < THREAD_COUNT; i++) { threads[i] = new Thread(() -> { for (int j = 0; j < OPERATION_COUNT; j++) { atomicLong.incrementAndGet(); } }); } long start1 = System.currentTimeMillis(); // 啟動(dòng)線程 for (Thread thread : threads) { thread.start(); } // 保證線程運(yùn)行完成 try { for (Thread thread : threads) { thread.join(); } } catch (InterruptedException e) { e.printStackTrace(); } long end1 = System.currentTimeMillis(); // 創(chuàng)建對(duì)LongAdder進(jìn)行操作的線程 for (int i = 0; i < THREAD_COUNT; i++) { threads[i] = new Thread(() -> { for (int j = 0; j < OPERATION_COUNT; j++) { longAdder.increment(); } }); } long start2 = System.currentTimeMillis(); // 啟動(dòng)線程 for (Thread thread : threads) { thread.start(); } // 保證線程運(yùn)行完成 try { for (Thread thread : threads) { thread.join(); } } catch (InterruptedException e) { e.printStackTrace(); } long end2 = System.currentTimeMillis(); System.out.println("AtomicLong運(yùn)行時(shí)間: " + (end1 - start1) + "ms, 運(yùn)行結(jié)果:" + atomicLong); System.out.println("LongAdder運(yùn)行時(shí)間: " + (end2 - start2) + "ms, 運(yùn)行結(jié)果:" + longAdder); } }
THREAD_COUNT = 100, OPERATION_COUNT = 1000
時(shí)的運(yùn)行結(jié)果
AtomicLong運(yùn)行時(shí)間: 40ms, 運(yùn)行結(jié)果:100000
LongAdder運(yùn)行時(shí)間: 57ms, 運(yùn)行結(jié)果:100000
Process finished with exit code 0
THREAD_COUNT = 100, OPERATION_COUNT = 10000
時(shí)的運(yùn)行結(jié)果
AtomicLong運(yùn)行時(shí)間: 108ms, 運(yùn)行結(jié)果:1000000
LongAdder運(yùn)行時(shí)間: 85ms, 運(yùn)行結(jié)果:1000000
Process finished with exit code 0
THREAD_COUNT = 100, OPERATION_COUNT = 1000000
時(shí)的運(yùn)行結(jié)果
AtomicLong運(yùn)行時(shí)間: 6909ms, 運(yùn)行結(jié)果:100000000
LongAdder運(yùn)行時(shí)間: 468ms, 運(yùn)行結(jié)果:100000000
Process finished with exit code 0
THREAD_COUNT = 10, OPERATION_COUNT = 1000000
時(shí)的運(yùn)行結(jié)果
AtomicLong運(yùn)行時(shí)間: 788ms, 運(yùn)行結(jié)果:10000000
LongAdder運(yùn)行時(shí)間: 162ms, 運(yùn)行結(jié)果10000000
Process finished with exit code 0
當(dāng)THREAD_COUNT * OPERATION_COUN
足夠小時(shí),AtomicInteger的性能會(huì)略高于LongAdder,而隨著THREAD_COUNT * OPERATION_COUN
的增加,LongAdder的性能更高,THREAD_COUNT * OPERATION_COUN
足夠大時(shí),LongAdder的性能遠(yuǎn)高于AtomicInteger。
AtomicLong的原子性自增操作,是通過(guò)CAS實(shí)現(xiàn)的。在競(jìng)爭(zhēng)線程數(shù)較少且每個(gè)線程的運(yùn)行所需時(shí)間較短的情況下,這樣做是合適的。但是如果線程競(jìng)爭(zhēng)激烈,會(huì)造成大量線程在原地打轉(zhuǎn)、不停嘗試去修改值,但是老是發(fā)現(xiàn)值被修改了,于是繼續(xù)自旋。 這樣浪費(fèi)了大量的CPU資源。
LongAdder在競(jìng)爭(zhēng)激烈時(shí),多個(gè)線程并不會(huì)一直自旋來(lái)修改值,而是采用了分段的思想,各個(gè)線程會(huì)分散累加到自己所對(duì)應(yīng)的Cell[]數(shù)組的某一個(gè)數(shù)組對(duì)象元素中,而不會(huì)大家共用一個(gè),把不同線程對(duì)應(yīng)到不同的Cell中進(jìn)行修改,降低了對(duì)臨界資源的競(jìng)爭(zhēng)。本質(zhì)上,是用空間換時(shí)間。
Java主要應(yīng)用于:1. web開(kāi)發(fā);2. Android開(kāi)發(fā);3. 客戶端開(kāi)發(fā);4. 網(wǎng)頁(yè)開(kāi)發(fā);5. 企業(yè)級(jí)應(yīng)用開(kāi)發(fā);6. Java大數(shù)據(jù)開(kāi)發(fā);7.游戲開(kāi)發(fā)等。
看完了這篇文章,相信你對(duì)“Java中常用的原子類(lèi)有哪些”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。