import java.util.Date;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 1.有界容器
* 2.队列容器
* 3.计数
*
*/
public class MyBlockQueue {
/**
* 队列容器
*/
private final LinkedList<Object> list = new LinkedList<Object>();
private final int begin = 0;
private final int end;
public MyBlockQueue(int length){
this.end = begin + length;
}
/**
* 计数器
*/
private AtomicInteger count = new AtomicInteger();
private final CountDownLatch countDownLatch = new CountDownLatch(1);
public int getSize(){
return count.get();
}
/**
* 放入元素
*/
public void push(Object obj) throws Exception{
while(count.get() == end){
countDownLatch.await();
}
list.add(obj);
System.out.println("生产元素:"+obj);
count.incrementAndGet();
countDownLatch.countDown();
}
/**
* 消费元素
*/
public Object pop() throws Exception{
while(count.get() == begin){
countDownLatch.await();
}
count.decrementAndGet();
countDownLatch.countDown();
Object obj = list.removeFirst();
System.out.println("消费:"+obj);
return obj;
}
public static void main(String[] args) {
final MyBlockQueue mq = new MyBlockQueue(5);
try {
mq.push("a");
mq.push(5.6);
mq.push(3);
mq.push(new Date());
mq.push(new Double(1));
System.out.println("当前容器的长度:" + mq.getSize());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = 3;
while(i > 0){
TimeUnit.SECONDS.sleep(4);
mq.push("f"+i);
i--;
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
while(mq.getSize()>0){
mq.pop();
TimeUnit.SECONDS.sleep(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t2");
t2.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 1.有界容器
* 2.队列容器
* 3.计数
*
*/
public class MyBlockQueue {
/**
* 队列容器
*/
private final LinkedList<Object> list = new LinkedList<Object>();
private final int begin = 0;
private final int end;
public MyBlockQueue(int length){
this.end = begin + length;
}
/**
* 计数器
*/
private AtomicInteger count = new AtomicInteger();
private final CountDownLatch countDownLatch = new CountDownLatch(1);
public int getSize(){
return count.get();
}
/**
* 放入元素
*/
public void push(Object obj) throws Exception{
while(count.get() == end){
countDownLatch.await();
}
list.add(obj);
System.out.println("生产元素:"+obj);
count.incrementAndGet();
countDownLatch.countDown();
}
/**
* 消费元素
*/
public Object pop() throws Exception{
while(count.get() == begin){
countDownLatch.await();
}
count.decrementAndGet();
countDownLatch.countDown();
Object obj = list.removeFirst();
System.out.println("消费:"+obj);
return obj;
}
public static void main(String[] args) {
final MyBlockQueue mq = new MyBlockQueue(5);
try {
mq.push("a");
mq.push(5.6);
mq.push(3);
mq.push(new Date());
mq.push(new Double(1));
System.out.println("当前容器的长度:" + mq.getSize());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = 3;
while(i > 0){
TimeUnit.SECONDS.sleep(4);
mq.push("f"+i);
i--;
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
while(mq.getSize()>0){
mq.pop();
TimeUnit.SECONDS.sleep(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
},"t2");
t2.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}