
课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
昆明达内培训的老师今天给大家讲条件队列。
package cn.xf.cp.ch14;
/**
*
*功能:使用条件队列
*时间:下午3:32:04
*文件:BoundedBuffer.java
*@author Administrator
*
* @param <V>
*/
public class BoundedBuffer<V> extends BaseBoundedBuffer<V>
{
public BoundedBuffer(int capacity)
{
super(capacity);
}
/**
*放入数据元素
* @param v
* @throws InterruptedException
*/
public synchronized void put(V v) throws InterruptedException
{
while(this.isFull())
{
//这里挂起程序,会释放锁
this.wait();
}
//如果队列不为满的,那么程序被唤醒之后从新获取锁
this.doPut(v);
//执行结束,唤醒其他队列
this.notifyAll();
}
public synchronized V take() throws InterruptedException
{
while(this.isEmpty())
{
this.wait();
}
V v = this.doTake();
this.notifyAll();
return v;
}
}