溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java中Atomic類之AtomicBoolean

發(fā)布時(shí)間:2020-07-09 15:34:36 來(lái)源:億速云 閱讀:153 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)java中Atomic類之AtomicBoolean,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

在java.util.concurrent.atomic包下,有AtomicBoolean , AtomicInteger, AtomicLong, AtomicReference等類,它們的基本特性就是在多線程環(huán)境下,執(zhí)行這些類實(shí)例包含的方法時(shí),具有排他性,即當(dāng)某個(gè)線程進(jìn)入方法,執(zhí)行其中的指令時(shí),不會(huì)被其他線程打斷,而別的線程就像自旋鎖一樣,一直等到該方法執(zhí)行完成,才由JVM從等待隊(duì)列中選擇一個(gè)另一個(gè)線程進(jìn)入。

舉例說(shuō)明

以AtomicBoolean為例,單線程執(zhí)行普通的方法(如下),不會(huì)出現(xiàn)線程問(wèn)題:

package com.secbro.test.atomic;

/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class NormalBoolean implements Runnable{

    public static boolean exits = false;

    private String name;

    public NormalBoolean(String name){
        this.name = name;
    }


    @Override
    public void run() {
        if(!exits){
            exits = true;
            System.out.println(name + ",step 1");
            System.out.println(name + ",step 2");
            System.out.println(name + ",step 3");
            exits = false;
        } else {
            System.out.println(name + ",step else");
        }
    }

    public static void main(String[] args) {
        new NormalBoolean("張三").run();
    }
}

然而,當(dāng)多線程執(zhí)行時(shí),就會(huì)出現(xiàn)在執(zhí)行判斷之后的命令時(shí),會(huì)有其他線程插入,變更exits的值。如下段代碼:

package com.secbro.test.atomic;
/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class NormalBoolean2 implements Runnable{
    public static boolean exits = false;
    private String name;
    public NormalBoolean2(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        if(!exits){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            exits = true;
            System.out.println(name + ",step 1");
            System.out.println(name + ",step 2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 3");
            exits = false;
        } else {
            System.out.println(name + ",step else");
        }
    }
}

同時(shí)執(zhí)行兩線程,打印結(jié)果為:

張三,step 1
李四,step 1
張三,step 2
李四,step 2
張三,step 3
李四,step 3

現(xiàn)在,使用AtomicBoolean就可以確保多線程的情況下安全的運(yùn)行,只有一個(gè)線程進(jìn)行業(yè)務(wù)處理。

package com.secbro.test.atomic;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class TestAtomicBoolean implements Runnable{

    public static AtomicBoolean exits = new AtomicBoolean(false);

    private String name;

    public TestAtomicBoolean(String name) {
        this.name = name;
    }

    @Override
    public void run() {

        if(exits.compareAndSet(false,true)){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 1");

            System.out.println(name + ",step 2");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + ",step 3");
            exits.set(false);
        } else {
            System.out.println(name + ",step else");
        }

    }
}

當(dāng)兩個(gè)線程執(zhí)行此類時(shí),打印結(jié)果如下:

張三,step else
李四,step 1
李四,step 2
李四,step 3

測(cè)試類:

package com.secbro.test.atomic;
/**
 * @author zhuzhisheng
 * @Description
 * @date on 2016/5/26.
 */
public class TestBoolean {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new NormalBoolean2("李四"));
        Thread thread2 = new Thread(new NormalBoolean2("張三"));
        thread1.start();
        thread2.start();
        //-------------------------------------------------------
        Thread thread3 = new Thread(new TestAtomicBoolean("李四"));
        Thread thread4 = new Thread(new TestAtomicBoolean("張三"));
        thread3.start();
        thread4.start();
    }
}

上述就是小編為大家分享的java中Atomic類之AtomicBoolean了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI