91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

發布時間:2020-08-03 16:07:36 來源:網絡 閱讀:509 作者:shayang88 欄目:編程語言

1、Condition介紹

1.1 Condition是對線程的wait,notify的增強

1.2 在ReentrantLock中他的實現類是AQS中的ConditionObject,實現了Condition接口,利用AQS的節點,實現了條件隊列。

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

2、案例分析

2.1 說明:Thread-1獲取鎖,然后await,釋放鎖;Thread-2獲得鎖,喚醒Thread-1,釋放鎖;Thread-1重新獲取鎖,釋放鎖。

2.2 代碼

2.2.1 Thread-1

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Task1 implements Runnable{

    private ReentrantLock lock;
    private Condition con;

    public Task1(ReentrantLock lock, Condition con) {
        this.lock = lock;
        this.con = con;
    }

    @Override
    public void run() {

        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "獲取到鎖....");
            System.out.println(Thread.currentThread().getName() + "開始阻塞....");
            con.await();
            System.out.println(Thread.currentThread().getName() + "重新獲取鎖,繼續執行....");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getName() + "釋放鎖....");
            lock.unlock();
        }
    }
}

2.2.2 Thread-2

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Task2 implements Runnable{

    private ReentrantLock lock;
    private Condition con;

    public Task2(ReentrantLock lock, Condition con) {
        this.lock = lock;
        this.con = con;
    }

    @Override
    public void run() {

        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "獲取到鎖....");
            System.out.println(Thread.currentThread().getName() + "喚醒Thread-1....");
            con.signal();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getName() + "釋放鎖....");
            lock.unlock();
        }
    }
}

2.2.3 啟動文件

import java.text.ParseException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

    public static void main(String[] args) throws ParseException, InterruptedException {
        ReentrantLock lock = new ReentrantLock(true);
        Condition condition = lock.newCondition();
        Thread t1 = new Thread(new Task1(lock,condition),"Thread-1");
        Thread.sleep(2000);
        Thread t2 = new Thread(new Task2(lock,condition),"Thread-2");
        t1.start();
        t2.start();
    }
}

2.2.4 運行結果

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

3、源碼分析

3.1 獲取鎖的源碼在上一篇已經分析過了,來看await操作:

public final void await() throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException(); //判斷線程是否中斷,中斷則拋出異常
        Node node = addConditionWaiter(); //當前線程包裝節點,放入【條件對列】,注意不是【等待隊列】
        int savedState = fullyRelease(node);//釋放鎖資源
        int interruptMode = 0;
        while (!isOnSyncQueue(node)) { //如果當前節點不在【等待隊列】
            LockSupport.park(this); //在這里阻塞,等待被喚醒,后面代碼喚醒前不執行
            if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                break;
        }
        if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
            interruptMode = REINTERRUPT;
        if (node.nextWaiter != null) // clean up if cancelled
            unlinkCancelledWaiters();
        if (interruptMode != 0)
            reportInterruptAfterWait(interruptMode);
    }

3.1.1 await()方法會釋放當前線程持有的鎖,就是fullyRelease方法的作用

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

3.1.2 線程A釋放了鎖,并進入條件對列,處于阻塞狀態。

3.2 Thread-2獲取到鎖后,調用signal方法喚醒Thread-1

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)
多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

final boolean transferForSignal(Node node) {
        /*
         * If cannot change waitStatus, the node has been cancelled.
         */
        //將節點的狀態修改為0,從【條件隊列】節點狀態轉換為【等待隊列】默認節點狀態0,節點可以插入【等待隊列】
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;

        /*
         * Splice onto queue and try to set waitStatus of predecessor to
         * indicate that thread is (probably) waiting. If cancelled or
         * attempt to set waitStatus fails, wake up to resync (in which
         * case the waitStatus can be transiently and harmlessly wrong).
         */
        Node p = enq(node);//將節點插入【等待隊列】
        int ws = p.waitStatus;
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL)) //將前驅節點設置為可喚醒狀態
            LockSupport.unpark(node.thread);//喚醒節點,也就是Thread-1
        return true;
    }

3.3 Thread-2釋放鎖

3.3.1Thread-2釋放鎖,會喚醒【等待隊列】的首節點,參看上一篇介紹(unparkSuccessor方法)

3.3.2 Thread-1繼續執行

多線程(十一、AQS原理-ReentrantLock的條件隊列Condition)

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

抚远县| 家居| 美姑县| 调兵山市| 安丘市| 大邑县| 凤庆县| 彭州市| 应城市| 荣成市| 奇台县| 桐梓县| 瑞昌市| 万山特区| 如东县| 若尔盖县| 喀什市| 法库县| 蓝田县| 方山县| 宿州市| 巫溪县| 景宁| 汽车| 河间市| 双鸭山市| 安远县| 黑龙江省| 清水河县| 如皋市| 右玉县| 饶阳县| 陇川县| 筠连县| 如东县| 五台县| 白河县| 泗阳县| 台北县| 慈溪市| 清远市|