1、何謂Atomic?java
Atomic一詞跟原子有點關係,後者曾被人認爲是最小物質的單位。計算機中的Atomic是指不能分割成若干部分的意思。若是一段代碼被認爲是Atomic,則表示這段代碼在執行過程當中,是不能被中斷的。一般來講,原子指令由硬件提供,供軟件來實現原子方法(某個線程進入該方法後,就不會被中斷,直到其執行完成)安全
在x86 平臺上,CPU提供了在指令執行期間對總線加鎖的手段。CPU芯片上有一條引線#HLOCK pin,若是彙編語言的程序中在一條指令前面加上前綴"LOCK",通過彙編之後的機器代碼就使CPU在執行這條指令的時候把#HLOCK pin的電位拉低,持續到這條指令結束時放開,從而把總線鎖住,這樣同一總線上別的CPU就暫時不能經過總線訪問內存了,保證了這條指令在多處理器環境中的原子性。多線程
2、JDK1.5的原子包:java.util.concurrent.atomic併發
這個包裏面提供了一組原子類。其基本的特性就是在多線程環境下,當有多個線程同時執行這些類的實例包含的方法時,具備排他性,即當某個線程進入方法,執行其中的指令時,不會被其餘線程打斷,而別的線程就像自旋鎖同樣,一直等到該方法執行完成,才由JVM從等待隊列中選擇一個另外一個線程進入,這只是一種邏輯上的理解。其實是藉助硬件的相關指令來實現的,不會阻塞線程(或者說只是在硬件級別上阻塞了)。其中的類能夠分紅4組dom
Atomic類的做用ide
2.1 AtomicBoolean , AtomicInteger, AtomicLong, AtomicReference函數
這四種基本類型用來處理布爾,整數,長整數,對象四種數據。this
2.1.1 1個例子-使用AtomicReference建立線程安全的堆棧atom
public class LinkedStack<T> { private AtomicReference<Node<T>> stacks = new AtomicReference<Node<T>>(); public T push(T e) { Node<T> oldNode, newNode; while (true) { //這裏的處理很是的特別,也是必須如此的。 oldNode = stacks.get(); newNode = new Node<T>(e, oldNode); if (stacks.compareAndSet(oldNode, newNode)) { return e; } } } public T pop() { Node<T> oldNode, newNode; while (true) { oldNode = stacks.get(); newNode = oldNode.next; if (stacks.compareAndSet(oldNode, newNode)) { return oldNode.object; } } } private static final class Node<T> { private T object; private Node<T> next; private Node(T object, Node<T> next) { this.object = object; this.next = next; } } }
2.1.2 幾個問題spa
Q1: compareAndSet和weakCompareAndSet的區別?
A1: 有人認爲這是個坑,由於這2個方法其中的內容是如出一轍的。疑惑ing(環境JDK1.6.0_20_b02)
Q2:volatile boolean和AtomicBoolean的區別?
Q3:volatile int和AtomicInteger的區別?
Q4:LazySet()和Set()的區別?
3、Atomic舉例
3.1 原子量實現的計數器
import java.util.HashSet; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class AtomicCounter { private AtomicInteger value = new AtomicInteger(); public int getValue() { return value.get(); } public int increase() { return value.incrementAndGet();// 內部使用死循環for(;;)調用compareAndSet(current, next) // return value.getAndIncrement(); } public int increase(int i) { return value.addAndGet(i);// 內部使用死循環for(;;)調用compareAndSet(current, next) // return value.getAndAdd(i); } public int decrease() { return value.decrementAndGet();// 內部使用死循環for(;;)調用compareAndSet(current, next) // return value.getAndDecrement(); } public int decrease(int i) { return value.addAndGet(-i);// 內部使用死循環for(;;)調用compareAndSet(current, next) // return value.addAndGet(-i); } public static void main(String[] args) { final AtomicCounter counter = new AtomicCounter(); ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { service.execute(new Runnable() { @Override public void run() { System.out.println(counter.increase()); } }); } service.shutdown(); } }
3.2 原子量實現的銀行取款
import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; public class Account { private AtomicLong balance; public Account(long money) { balance = new AtomicLong(money); System.out.println("Total Money:" + balance); } public void deposit(long money) { balance.addAndGet(money); } public void withdraw(long money) { for (; ; ) {//保證即時同一時間有人也在取款也能夠再次嘗試取款,若是不須要併發嘗試取款,能夠去掉這句 long oldValue = balance.get(); if (oldValue < money) { System.out.println(Thread.currentThread().getName() + " 餘額不足! 餘額:" + balance); break; } try {Thread.sleep(new Random().nextInt(1000));} catch (Exception e) { }// 模擬取款時間 if (balance.compareAndSet(oldValue, oldValue - money)) { System.out.println(Thread.currentThread().getName() + " 取款 " + money + " 成功! 餘額:" + balance); break; } System.out.println(Thread.currentThread().getName() + " 遇到併發,再次嘗試取款!"); } } public static void main(String[] args) { final Account account = new Account(1000); ExecutorService pool = Executors.newCachedThreadPool(); int i = 0; while (i++ < 13) { pool.execute(new Runnable() { @Override public void run() { account.withdraw(100); } }); } pool.shutdown(); } }