package com.tl.skyLine.thread;
import java.util.concurrent.ArrayBlockingQueue;
/**
* Created by tl on 17/3/3.
*/
public class BlockingQueueTest {
//final成员变量表示常量,只能被赋值一次,赋值后值不再改变。
private static final int queueSize = 5;
private static final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(queueSize);
private static final int produceSpeed = 2000;//生产速度(越小越快)
private static final int consumeSpeed = 10;//消费速度(越小越快)
//生产者
public static class Producer implements Runnable {
@Override
public void run() {
while (true) {
try {
System.out.println("老板准备炸油条了,架子上还能放:" + (queueSize - queue.size()) + "根油条");
queue.put("1根油条");
System.out.println("老板炸好了1根油条,架子上还能放:" + (queueSize - queue.size()) + "根油条");
Thread.sleep(produceSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者
public static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
try {
System.out.println("A 准备买油条了,架子上还剩" + queue.size() + "根油条");
queue.take();
System.out.println("A 买到1根油条,架子上还剩" + queue.size() + "根油条");
Thread.sleep(consumeSpeed);
System.out.println("B 准备买油条了,架子上还剩" + queue.size() + "根油条");
queue.take();
System.out.println("B 买到1根油条,架子上还剩" + queue.size() + "根油条");
Thread.sleep(consumeSpeed);
System.out.println("C 准备买油条了,架子上还剩" + queue.size() + "根油条");
queue.take();
System.out.println("C 买到1根油条,架子上还剩" + queue.size() + "根油条");
Thread.sleep(consumeSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread producer = new Thread(new Producer());
Thread consumer = new Thread(new Consumer());
producer.start();
consumer.start();
}
}
老板准备炸油条了,架子上还能放:5根油条 老板炸好了1根油条,架子上还能放:4根油条 A 准备买油条了,架子上还剩1根油条 A 买到1根油条,架子上还剩0根油条 B 准备买油条了,架子上还剩0根油条 老板准备炸油条了,架子上还能放:5根油条 老板炸好了1根油条,架子上还能放:4根油条 B 买到1根油条,架子上还剩0根油条 C 准备买油条了,架子上还剩0根油条 老板准备炸油条了,架子上还能放:5根油条 老板炸好了1根油条,架子上还能放:4根油条 C 买到1根油条,架子上还剩0根油条 A 准备买油条了,架子上还剩0根油条 老板准备炸油条了,架子上还能放:5根油条 老板炸好了1根油条,架子上还能放:4根油条 A 买到1根油条,架子上还剩0根油条 B 准备买油条了,架子上还剩0根油条 老板准备炸油条了,架子上还能放:5根油条 老板炸好了1根油条,架子上还能放:4根油条
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
/** Main lock guarding all access */ final ReentrantLock lock; /** Condition for waiting takes */ private final Condition notEmpty; /** Condition for waiting puts */ private final Condition notFull;
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
notFull.signal();
return x;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有