十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
创新互联-专业网站定制、快速模板网站建设、高性价比嘉鱼网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式嘉鱼网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖嘉鱼地区。费用合理售后完善,十载实体公司更值得信赖。
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;
}