您好,登錄后才能下訂單哦!
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();
}
}
}
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();
}
}
}
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();
}
}
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);
}
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。