您好,登錄后才能下訂單哦!
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() + "重新獲取鎖,繼續(xù)執(zhí)行....");
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(); //當前線程包裝節(jié)點,放入【條件對列】,注意不是【等待隊列】
int savedState = fullyRelease(node);//釋放鎖資源
int interruptMode = 0;
while (!isOnSyncQueue(node)) { //如果當前節(jié)點不在【等待隊列】
LockSupport.park(this); //在這里阻塞,等待被喚醒,后面代碼喚醒前不執(zhí)行
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.
*/
//將節(jié)點的狀態(tài)修改為0,從【條件隊列】節(jié)點狀態(tài)轉(zhuǎn)換為【等待隊列】默認節(jié)點狀態(tài)0,節(jié)點可以插入【等待隊列】
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);//將節(jié)點插入【等待隊列】
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL)) //將前驅(qū)節(jié)點設(shè)置為可喚醒狀態(tài)
LockSupport.unpark(node.thread);//喚醒節(jié)點,也就是Thread-1
return true;
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。