Java多線程 原子性操作類的使用
前言:
在java5以後,我們接觸到瞭線程原子性操作,也就是在修改時我們隻需要保證它的那個瞬間是安全的即可,經過相應的包裝後可以再處理對象的並發修改,本文總結一下Atomic系列的類的使用方法,其中包含:
1. 基本類型的使用
public class AtomicTest { /** * 常見的方法列表 * * @see AtomicInteger#get() 直接返回值 * @see AtomicInteger#getAndAdd(int) 增加指定的數據,返回變化前的數據 * @see AtomicInteger#getAndDecrement() 減少1,返回減少前的數據 * @see AtomicInteger#getAndIncrement() 增加1,返回增加前的數據 * @see AtomicInteger#getAndSet(int) 設置指定的數據,返回設置前的數據 * @see AtomicInteger#addAndGet(int) 增加指定的數據後返回增加後的數據 * @see AtomicInteger#decrementAndGet() 減少1,返回減少後的值 * @see AtomicInteger#incrementAndGet() 增加1,返回增加後的值 * @see AtomicInteger#lazySet(int) 僅僅當get時才會set * @see AtomicInteger#compareAndSet(int, int) 嘗試新增後對比,若增加成功則返回true否則返回false **/ public static void main(String[] args) { final AtomicTicket ticket = new AtomicTicket(); for (int i = 0; i < 3; i++) { new Thread(new Runnable() { @Override public void run() { while (ticket.getCount() > 0) { System.out.println(Thread.currentThread().getName() + " count: " + ticket.decrement()); } } }).start(); } } } class AtomicTicket { public AtomicInteger count = new AtomicInteger(100); public int decrement() { return count.getAndDecrement(); } public int getCount() { return count.get(); } }
Thread-0 count: 100
Thread-2 count: 98
Thread-1 count: 99
Thread-2 count: 96
Thread-0 count: 97
Thread-2 count: 94
Thread-2 count: 92
Thread-1 count: 95
中間省略…
Thread-1 count: 12
Thread-2 count: 7
Thread-0 count: 9
Thread-2 count: 5
Thread-1 count: 6
Thread-2 count: 3
Thread-0 count: 4
Thread-2 count: 1
Thread-1 count: 2
2. 數組類型的使用
public class AtomicIntegerArrayTest { /** * 常見的方法列表 * @see AtomicIntegerArray#addAndGet(int, int) 執行加法,第一個參數為數組的下標,第二個參數為增加的數量,返回增加後的結果 * @see AtomicIntegerArray#compareAndSet(int, int, int) 對比修改,參數1:數組下標,參數2:原始值,參數3,修改目標值,修改成功返回true否則false * @see AtomicIntegerArray#decrementAndGet(int) 參數為數組下標,將數組對應數字減少1,返回減少後的數據 * @see AtomicIntegerArray#incrementAndGet(int) 參數為數組下標,將數組對應數字增加1,返回增加後的數據 * * @see AtomicIntegerArray#getAndAdd(int, int) 和addAndGet類似,區別是返回值是變化前的數據 * @see AtomicIntegerArray#getAndDecrement(int) 和decrementAndGet類似,區別是返回變化前的數據 * @see AtomicIntegerArray#getAndIncrement(int) 和incrementAndGet類似,區別是返回變化前的數據 * @see AtomicIntegerArray#getAndSet(int, int) 將對應下標的數字設置為指定值,第二個參數為設置的值,返回是變化前的數據 */ private final static AtomicIntegerArray ATOMIC_INTEGER_ARRAY = new AtomicIntegerArray(new int[]{1,2,3,4,5,6,7,8,9,10}); public static void main(String []args) throws InterruptedException { Thread []threads = new Thread[10]; for(int i = 0 ; i < 10 ; i++) { final int index = i; threads[i] = new Thread() { public void run() { int original = ATOMIC_INTEGER_ARRAY.get(index); int result = ATOMIC_INTEGER_ARRAY.addAndGet(index, index + 1); System.out.println("currentThread:" + Thread.currentThread().getName() + " , 原始值為:" + original + ",增加後的結果為:" + result); } }; threads[i].start(); } for(Thread thread : threads) { thread.join(); } System.out.println("=========================>\n執行已經完成,結果列表:"); for(int i = 0 ; i < ATOMIC_INTEGER_ARRAY.length() ; i++) { System.out.println(ATOMIC_INTEGER_ARRAY.get(i)); } } }
currentThread:Thread-0 , 原始值為:1,增加後的結果為:2
currentThread:Thread-3 , 原始值為:4,增加後的結果為:8
currentThread:Thread-2 , 原始值為:3,增加後的結果為:6
currentThread:Thread-1 , 原始值為:2,增加後的結果為:4
currentThread:Thread-5 , 原始值為:6,增加後的結果為:12
currentThread:Thread-4 , 原始值為:5,增加後的結果為:10
currentThread:Thread-6 , 原始值為:7,增加後的結果為:14
currentThread:Thread-7 , 原始值為:8,增加後的結果為:16
currentThread:Thread-8 , 原始值為:9,增加後的結果為:18
currentThread:Thread-9 , 原始值為:10,增加後的結果為:20
=========================>執行已經完成,結果列表:
2
4
6
8
10
12
14
16
18
20
3. 引用類型的使用
public class AtomicReferenceTest { public static void main(String[] args) { People people1 =new People("Bom", 0); People people2 =new People("Tom",10); //先初始化一個值,如果不初始化則默認值為null AtomicReference<People> reference = new AtomicReference<>(people1); People people3 = reference.get(); if (people3.equals(people1)) { System.out.println("people3:" + people3); } else { System.out.println("else:" + people3); } /** * 當前值:拿當前值和reference.get()獲取到的值去比較,如果相等則true並更新值為期望值 * 期望值:如果返回true則更新為期望值,如果返回false則不更新值 */ boolean b = reference.compareAndSet(null, people2); System.out.println("myClass.main-"+b+"--"+reference.get()); boolean b1 = reference.compareAndSet(people1, people2); System.out.println("myClass.main-"+b1+"--"+reference.get()); new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); People people = reference.get(); people.setName("Tom"+Thread.currentThread().getName()); people.setAge(people.getAge()+1); reference.getAndSet(people); System.out.println(Thread.currentThread().getName()+reference.get().toString()); } }).start(); new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); People people = reference.get(); people.setName("Tom"+Thread.currentThread().getName()); people.setAge(people.getAge()+4); reference.getAndSet(people); System.out.println(Thread.currentThread().getName()+reference.get().toString()); } }).start(); } } class People { private String name; private int age; public People(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People{" + "name='" + name + ''' + ", age=" + age + '}'; } }
4.字段類型的使用
public class AtomicIntegerFieldUpdaterTest { /** * 可以直接訪問對應的變量,進行修改和處理 * 條件:要在可訪問的區域內,如果是private或挎包訪問default類型以及非父親類的protected均無法訪問到 * 其次訪問對象不能是static類型的變量(因為在計算屬性的偏移量的時候無法計算),也不能是final類型的變量(因為根本無法修改),必須是普通的成員變量 * <p> * 方法(說明上和AtomicInteger幾乎一致,唯一的區別是第一個參數需要傳入對象的引用) * * @see AtomicIntegerFieldUpdater#addAndGet(Object, int) * @see AtomicIntegerFieldUpdater#compareAndSet(Object, int, int) * @see AtomicIntegerFieldUpdater#decrementAndGet(Object) * @see AtomicIntegerFieldUpdater#incrementAndGet(Object) * @see AtomicIntegerFieldUpdater#getAndAdd(Object, int) * @see AtomicIntegerFieldUpdater#getAndDecrement(Object) * @see AtomicIntegerFieldUpdater#getAndIncrement(Object) * @see AtomicIntegerFieldUpdater#getAndSet(Object, int) */ public final static AtomicIntegerFieldUpdater<A> ATOMIC_INTEGER_UPDATER = AtomicIntegerFieldUpdater.newUpdater(A.class, "intValue"); public static void main(String[] args) { final A a = new A(); for (int i = 0; i < 10; i++) { new Thread() { public void run() { System.out.println( Thread.currentThread().getName() + " " + ATOMIC_INTEGER_UPDATER.get(a)); ATOMIC_INTEGER_UPDATER.addAndGet(a, 11); System.out.println( Thread.currentThread().getName() + " " + ATOMIC_INTEGER_UPDATER.get(a)); if (ATOMIC_INTEGER_UPDATER.compareAndSet(a, ATOMIC_INTEGER_UPDATER.get(a), 120)) { System.out.println(Thread.currentThread().getName() + " 對應的值做瞭修改!"); } System.out.println( Thread.currentThread().getName() + " " + ATOMIC_INTEGER_UPDATER.get(a)); } }.start(); } } static class A { volatile int intValue = 100; } }
Thread-0 100
Thread-2 100
Thread-1 100
Thread-2 122
Thread-3 111
Thread-5 120
Thread-0 111
Thread-5 142
Thread-3 131
Thread-2 對應的值做瞭修改!
Thread-2 120
Thread-8 120
Thread-4 133
Thread-1 133
Thread-9 142
Thread-4 142
Thread-4 對應的值做瞭修改!
Thread-8 131
Thread-3 對應的值做瞭修改!
Thread-3 120
Thread-7 120
Thread-7 131
Thread-5 對應的值做瞭修改!
Thread-5 120
Thread-6 120
Thread-0 對應的值做瞭修改!
Thread-6 131
Thread-7 對應的值做瞭修改!
Thread-8 對應的值做瞭修改!
Thread-4 120
Thread-9 131
Thread-1 對應的值做瞭修改!
Thread-9 對應的值做瞭修改!
Thread-8 120
Thread-7 120
Thread-6 對應的值做瞭修改!
Thread-0 131
Thread-6 120
Thread-9 120
Thread-1 120
到此這篇關於Java多線程 原子性操作類的使用的文章就介紹到這瞭,更多相關Java多線程 原子性操作類內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!