import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 与重入锁相关联的Condition
*/
public class ConditionDemo implements Runnable{
public static ReentrantLock lock = new ReentrantLock();
public static Condition condition = lock.newCondition();
@Override
public void run() {
try {
lock.lock();
condition.await();//使当前线程等待,同时释放当前锁
System.out.println("Thread is going on");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException{
ConditionDemo t = new ConditionDemo();
Thread t1 = new Thread(t);
t1.start();
Thread.sleep(2000);
//通知线程t1继续执行
lock.lock();//调用signal()的方法时,线程要先获得相关的锁
condition.signal();//唤醒一个在等待中的线程
lock.unlock();//signal()调用后释放相关的锁给被唤醒的线程
}
}