import java.util.concurrent.locks.*;
import net.jcip.annotations.*;
/**
* ConditionBoundedBuffer
* <p/>
* Bounded buffer using explicit condition variables
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class ConditionBoundedBuffer<T>
{
protected final Lock lock = new ReentrantLock();
// CONDITION PREDICATE: notFull (count < items.length)
private final Condition notFull = lock.newCondition();
// CONDITION PREDICATE: notEmpty (count > 0)
private final Condition notEmpty = lock.newCondition();
private static final int BUFFER_SIZE = 100;
@SuppressWarnings("unchecked")
@GuardedBy("lock")
private final T[] items = (T[]) new Object[BUFFER_SIZE];
@GuardedBy("lock")
private int tail, head, count;
// BLOCKS-UNTIL: notFull
public void put(T x) throws InterruptedException
{
lock.lock();
try
{
while (count == items.length)
notFull.await();
items[tail] = x;
if (++tail == items.length)
tail = 0;
++count;
notEmpty.signal();
}
finally
{
lock.unlock();
}
}
// BLOCKS-UNTIL: notEmpty
public T take() throws InterruptedException
{
lock.lock();
try
{
while (count == 0)
notEmpty.await();
T x = items[head];
items[head] = null;
if (++head == items.length)
head = 0;
--count;
notFull.signal();
return x;
}
finally
{
lock.unlock();
}
}
}
分享到:
相关推荐
spring缓存中用conditon限制放入缓存的数据。
主要介绍了Spring实战之缓存使用condition操作,结合实例形式分析了Spring缓存使用condition具体配置、属性、领域模型等相关操作技巧与注意事项,需要的朋友可以参考下
主要介绍了Python多线程编程(七):使用Condition实现复杂同步,本文讲解通过很著名的“生产者-消费者”模型来来演示在Python中使用Condition实现复杂同步,需要的朋友可以参考下
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
@Cacheable:(cacheNames/value缓存名字,key支持sqel,keyGenerator生成器,指定缓存器,condition支持sqel的缓存条件,unless为true时候不缓存,sync是否异步)注意类内部的调用不触发缓存,所以一定是service...
下面小编就为大家带来一篇Linux C++ 使用condition实现阻塞队列的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Python线程协作threading.Condition实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、#加变量名字,就可以表示变量了, 比如 #id2、使用对象中的变量 #user.id 或者#user.getId() 都可以condition:触发条件,只
1. 使用wx:if={{condition}} wx:else实现图标的切换效果; 2. 为图片绑定点击事件bindtap=toCollect,两个image标签都要绑定! <image wx:if={{collection}} src=/images/icon/pic1.png bindtap=toCollect></image...
Run Condition Plugin 1.0 hpi
前端项目-condition,Advanced condition library
线程通信(Condition)实例,完整的代码文件,你只需要编译运行即可,就可以看看结果,然后分析。
MyBatisPlus条件构造器Condition的用法示例代码
通过@Bean和@Condition 注解自定义对于的condition里面根据自定义的条件实现指定类注入到spring中;@ConditionalOnProperty可以根据配置文件中的 属性值不同将不同的类注入到spring中 该资源中案例完整,代码简单移动
C++ 11 ,thread ,atomic ,mutex ,condition_variable 。 程序特性: 1.每个实例支持读写双线程。 2.数据一致。 3.ringbuffer为空时,读取时候等待。直到有数据写入,再继续读取。 4.当写入数据的长度大于ring...
通过java语言编写的生产者消费者,实现方法为lock类和condition类的配合完成。
详细介绍了线程同步条件变量condition_variable的使用和它的源码,涉及到unique_lock, mutex, lock_guard, 虚假唤醒和惊群效应。
C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装。不过里面也有很多细节值得学习。 std::mutex 先来看下std::mutex: 包增了一个pthread_mutex_t __m_,很简单,每个函数该干嘛...
SQL_CONDITION组成WHERE条件类