java線程:Atomic的含義及示例

字號:


    Atomic概念
    計算機中的Atomic是指不能分割成若干部分的意思。如果一段代碼被認為是Atomic,則表示這段代碼在執(zhí)行過程中,是不能被中斷的。通常來說,原子指令由硬件提供,供軟件來實現(xiàn)原子方法(某個線程進入該方法后,就不會被中斷,直到其執(zhí)行完成)
    在x86 平臺上,CPU提供了在指令執(zhí)行期間對總線加鎖的手段。CPU芯片上有一條引線#HLOCK pin,如果匯編語言的程序中在一條指令前面加上前綴"LOCK",經(jīng)過匯編以后的機器代碼就使CPU在執(zhí)行這條指令的時候把#HLOCK pin的電位拉低,持續(xù)到這條指令結(jié)束時放開,從而把總線鎖住,這樣同一總線上別的CPU就暫時不能通過總線訪問內(nèi)存了,保證了這條指令在多處理器環(huán)境中的原子性
    JDk中的java.util.concurrent.atomic
    基本的特性就是在多線程環(huán)境下,當有多個線程同時執(zhí)行這些類的實例包含的方法時,具有排他性,即當某個線程進入方法,執(zhí)行其中的指令時,不會被其他線程打斷,而別的線程就像自旋鎖一樣,一直等到該方法執(zhí)行完成,才由JVM從等待隊列中選擇一個另一個線程進入,這只是一種邏輯上的理解。實際上是借助硬件的相關(guān)指令來實現(xiàn)的,不會阻塞線程(或者說只是在硬件級別上阻塞了)。
    其中的類可以分成4組
    AtomicBoolean,AtomicInteger,AtomicLong,AtomicReference
    AtomicIntegerArray,AtomicLongArray
    AtomicLongFieldUpdater,AtomicIntegerFieldUpdater,AtomicReferenceFieldUpdater
    AtomicMarkableReference,AtomicStampedReference,AtomicReferenceArray
    Atomic類的作用
    使得讓對單一數(shù)據(jù)的操作,實現(xiàn)了原子化
    使用Atomic類構(gòu)建復(fù)雜的,無需阻塞的代碼
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicBoolean;
    public class Test {
    public static void main(String[] args) throws InterruptedException {
    Test test = new Test();
    test.testAtomicBoolean();
    }
    private AtomicBoolean wakeupPending = new AtomicBoolean(false);
    private AtomicBoolean initialized = new AtomicBoolean(false);
    public void testAtomicBoolean() throws InterruptedException{
    // 實現(xiàn)只有一個線程在運行
    ExecutorService service = Executors.newCachedThreadPool();
    service.execute(new Worker("aa"));
    service.execute(new Worker("bb"));
    service.execute(new Worker("cc"));
    TimeUnit.SECONDS.sleep(2);
    }
    private class Worker implements Runnable {
    private String name;
    public Worker(String name) {
    this.name = name;
    init();
    }
    public void init() {
    if (initialized.compareAndSet(false, true)) {
    System.out.println("實現(xiàn)只初始化一次的功能");
    }
    }
    public void run() {
    while (true) {
    if (wakeupPending.compareAndSet(false, true)) {
    System.out.println(name + " enter");
    System.out.println(name + " working");
    System.out.println(name + " leave");
    try {
    TimeUnit.MILLISECONDS.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    wakeupPending.set(false);
    } else {
    // System.out.println(name + " give up");
    }
    }
    }
    }
    }
    訪問對2個或2個以上的atomic變量(或者對單個atomic變量進行2次或2次以上的操作)通常認為是需要同步的,以達到讓這些操作能被作為一個原子單元。
    2.1 AtomicBoolean , AtomicInteger, AtomicLong, AtomicReference
    這四種基本類型用來處理布爾,整數(shù),長整數(shù),對象四種數(shù)據(jù)。
    構(gòu)造函數(shù)(兩個構(gòu)造函數(shù))
    默認的構(gòu)造函數(shù):初始化的數(shù)據(jù)分別是false,0,0,null
    帶參構(gòu)造函數(shù):參數(shù)為初始化的數(shù)據(jù)
    set( )和get( )方法:可以原子地設(shè)定和獲取atomic的數(shù)據(jù)。類似于volatile,保證數(shù)據(jù)會在主存中設(shè)置或讀取
    getAndSet( )方法
    原子的將變量設(shè)定為新數(shù)據(jù),同時返回先前的舊數(shù)據(jù)
    其本質(zhì)是get( )操作,然后做set( )操作。盡管這2個操作都是atomic,但是他們合并在一起的時候,就不是atomic。在Java的源程序的級別上,如果不依賴synchronized的機制來完成這個工作,是不可能的。只有依靠native方法才可以。
    compareAndSet( ) 和weakCompareAndSet( )方法
    這兩個方法都是conditional modifier方法。這2個方法接受2個參數(shù),一個是期望數(shù)據(jù)(expected),一個是新數(shù)據(jù)(new);如果atomic里面的數(shù)據(jù)和期望數(shù)據(jù)一致,則將新數(shù)據(jù)設(shè)定給atomic的數(shù)據(jù),返回true,表明成功;否則就不設(shè)定,并返回false。
    對于AtomicInteger、AtomicLong還提供了一些特別的方法。getAndIncrement( )、incrementAndGet( )、getAndDecrement( )、decrementAndGet ( )、addAndGet( )、getAndAdd( )以實現(xiàn)一些加法,減法原子操作。(注意 --i、++i不是原子操作,其中包含有3個操作步驟:第一步,讀取i;第二步,加1或減1;第三步:寫回內(nèi)存)