并发编程复习(六):使用wait和notify模拟阻塞队列
2017-09-21 20:20
633 查看
话不多说,代码如下
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 使用wait和notify模拟阻塞队列......
*/
public class MyQueue {
private int minSize = 0;
private int maxSize;
public MyQueue(int size) {
this.maxSize = size;
}
private LinkedList<Object> list = new LinkedList<>();
private AtomicInteger count = new AtomicInteger();
private Object lock = new Object();
public void put(Object obj) {
synchronized (lock) {
while (count.intValue() == maxSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(obj);
System.out.println("添加的元素为:"+obj);
count.incrementAndGet();
lock.notify();
}
}
public Object take() {
Object rel = null;
synchronized (lock) {
while (count.intValue() == minSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
rel = list.removeFirst();
System.out.println("移除的元素为:"+rel);
count.decrementAndGet();
lock.notify();
}
return rel;
}
public Integer getSize() {
return list.size();
}
public static void main(String args[]) {
final MyQueue mq = new MyQueue(5);
mq.put("a");
mq.put("b");
mq.put("c");
mq.put("d");
mq.put("e");
System.out.println("队列中的对象个数为:"+mq.getSize());
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
mq.put("f");
mq.put("g");
}
},"thread01:");
thread1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Object o = mq.take();
System.out.println("取得的元素是:"+o);
Object o1 = mq.take();
System.out.println("取得的元素是:"+o1);
}
},"thread02:");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
输出结果:
添加的元素为:a
添加的元素为:b
添加的元素为:c
添加的元素为:d
添加的元素为:e
队列中的对象个数为:5
移除的元素为:a
添加的元素为:f
取得的元素是:a
移除的元素为:b
取得的元素是:b
添加的元素为:g
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 使用wait和notify模拟阻塞队列......
*/
public class MyQueue {
private int minSize = 0;
private int maxSize;
public MyQueue(int size) {
this.maxSize = size;
}
private LinkedList<Object> list = new LinkedList<>();
private AtomicInteger count = new AtomicInteger();
private Object lock = new Object();
public void put(Object obj) {
synchronized (lock) {
while (count.intValue() == maxSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(obj);
System.out.println("添加的元素为:"+obj);
count.incrementAndGet();
lock.notify();
}
}
public Object take() {
Object rel = null;
synchronized (lock) {
while (count.intValue() == minSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
rel = list.removeFirst();
System.out.println("移除的元素为:"+rel);
count.decrementAndGet();
lock.notify();
}
return rel;
}
public Integer getSize() {
return list.size();
}
public static void main(String args[]) {
final MyQueue mq = new MyQueue(5);
mq.put("a");
mq.put("b");
mq.put("c");
mq.put("d");
mq.put("e");
System.out.println("队列中的对象个数为:"+mq.getSize());
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
mq.put("f");
mq.put("g");
}
},"thread01:");
thread1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Object o = mq.take();
System.out.println("取得的元素是:"+o);
Object o1 = mq.take();
System.out.println("取得的元素是:"+o1);
}
},"thread02:");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
输出结果:
添加的元素为:a
添加的元素为:b
添加的元素为:c
添加的元素为:d
添加的元素为:e
队列中的对象个数为:5
移除的元素为:a
添加的元素为:f
取得的元素是:a
移除的元素为:b
取得的元素是:b
添加的元素为:g
相关文章推荐
- 模拟阻塞队列-使用notify和wait
- 【Java并发编程】:使用wait/notify/notifyAll实现线程间通信
- 使用notiy和wait模拟阻塞队列
- 用wait和notify模拟阻塞队列
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- Java并发编程笔记 使用阻塞队列实现生产者-消费者模型
- java并发编程学习: 阻塞队列 使用 及 实现原理
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明(r)
- 并发编程复习(四):wait和notify解析
- 并发编程-wait,notify实现队列功能
- 转:【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- wait/notify模拟阻塞队列
- 【Java并发编程】:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- 使用wait()和notify()模拟Queue(队列)
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- notify和notifyAll的区别及阻塞队列模拟实现
- wait, notify, notifyAll, 简单数组模拟队列实现读者写者问题。