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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。