重庆公司注册服务,惠州优化怎么做seo,学做网站格式工厂,2021年最为成功的营销案例当线程访问某个对象时#xff0c;发现条件不满足#xff0c;暂时挂起等待条件满足时再次访问。Guarded Suspension模式是一个非常基础的模式#xff0c;主要关注#xff08;临界值#xff09;不满足时将操作的线程正确挂起#xff0c;以防止出现数据不一致或者操作超过临… 当线程访问某个对象时发现条件不满足暂时挂起等待条件满足时再次访问。Guarded Suspension模式是一个非常基础的模式主要关注临界值不满足时将操作的线程正确挂起以防止出现数据不一致或者操作超过临界值的控制范围。它是很多线程设计模式的基础。 示例代码
import java.util.LinkedList;public class GuardedSuspensionQueue {
private final LinkedListInteger queuenew LinkedList();
private final int LIMIT100;public void offer(Integer data) throws InterruptedException{
synchronized(this) {
while(queue.size()LIMIT) {
this.wait();
}
queue.addLast(data);
this.notifyAll();
}
}public Integer take() throws InterruptedException{
synchronized(this) {
while(queue.isEmpty()) {
this.wait();
}
this.notifyAll();
return queue.removeFirst();
}
}
}