import java.util.concurrent.ArrayBlockingQueue;
/**
* @author 作者:徐剑 E-mail:anxu_2013@163.com
* @version 创建时间:2016年3月20日 下午12:52:53
* 类说明
*/
public class BlockingQueue
{
public static void main(String[] args) throws InterruptedException
{
java.util.concurrent.BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(5);
for (int i = 0; i < 10; i++)
{
// 将指定元素添加到此队列中
blockingQueue.put("加入元素" + i);
System.out.println("向阻塞队列中添加了元素:" + i);
}
System.out.println("程序到此运行结束,即将退出----");
}
}
public class BlockingQueue
{
public static void main(String[] args) throws InterruptedException
{
java.util.concurrent.BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(5);
for (int i = 0; i < 10; i++)
{
// 将指定元素添加到此队列中
blockingQueue.put("加入元素" + i);
System.out.println("向阻塞队列中添加了元素:" + i);
if(i>=4)
System.out.println("移除队首元素"+blockingQueue.take());
}
System.out.println("程序到此运行结束,即将退出----");
}
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
/** 底层存储结构-数组 */
final Object[] items;
/** 队首元素下标 */
int takeIndex;
/** 队尾元素下标 */
int putIndex;
/**队列元素总数 */
int count;
/** 重入锁 */
final ReentrantLock lock;
/** notEmpty等待条件 */
private final Condition notEmpty;
/** notFull等待条件 */
private final Condition notFull;
/**
* Shared state for currently active iterators, or null if there
* are known not to be any. Allows queue operations to update
* iterator state.
*/
transient Itrs itrs = null;
public void put(E e) throws InterruptedException
{
//先检查e是否为空
checkNotNull(e);
//获取锁
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try
{
//当队列已满,进入条件等待
while (count == items.length)
notFull.await();
//队列不满,进行入队列操作
enqueue(e);
}
finally
{
//释放锁
lock.unlock();
}
}
private void enqueue(E x)
{
final Object[] items = this.items;
//队尾入队
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
//队列总数+1
count++;
//notempty条件的等待集中随机选择一个线程,解除其阻塞状态
notEmpty.signal();
}
public E take() throws InterruptedException
{
//获取锁
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try
{
//队列为空
while (count == 0)
//线程加入notEmpty条件等待集
notEmpty.await();
//非空,出队列
return dequeue();
} finally
{
//释放锁
lock.unlock();
}
}
/**
* @author 作者:徐剑 E-mail:anxu_2013@163.com
* @version 创建时间:2016年3月20日 下午2:21:55
* 类说明:阻塞队列实现的消费者-生产者模式
*/
public class Test
{
private int queueSize = 10;
private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(queueSize);
public static void main(String[] args)
{
Test test = new Test();
Producer producer = test.new Producer();
Consumer consumer = test.new Consumer();
producer.start();
consumer.start();
}
class Consumer extends Thread
{
@Override
public void run()
{
consume();
}
private void consume()
{
while (true)
{
try
{
queue.take();
System.out.println("从队列取走一个元素,队列剩余" + queue.size() + "个元素");
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class Producer extends Thread
{
@Override
public void run()
{
produce();
}
private void produce()
{
while (true)
{
try
{
queue.put(1);
System.out.println("向队列取中插入一个元素,队列剩余空间:"+ (queueSize - queue.size()));
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有