生产者消费者模式---java
2016-03-04 16:22
274 查看
/**
*
*/
import java.util.*;
public class PCModel {
public static void main(String[] args) {
Box box = new Box();
PAdd p1 = new PAdd(box);
PRemove p2 = new PRemove(box);
PAdd p3= new PAdd(box);
PRemove p4 = new PRemove(box);
PAdd p5 = new PAdd(box);
PRemove p6 = new PRemove(box);
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
}
}
class Box {
private static final int max = 10;
private List<Integer> list;
public Box(){
list = new ArrayList<>();
}
public void add() {
synchronized (list) {
while(list.size() >= max) {
System.out.println("盒子已满,等待删除!");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(1);
System.out.println("当前容量:" + list.size() + " --->" + Thread.currentThread().getName());
list.notifyAll();
}
}
public void remove() {
synchronized (list) {
while(list.size()==0){
System.out.println("盒子中没有产品,等待添加!");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove(0);
System.out.println("当前容量:" + list.size() + " --->" + Thread.currentThread().getName());
list.notifyAll();
}
}
}
class PAdd extends Thread{
private Box box;
public PAdd(Box box) {
this.box = box;
}
public void add() {
box.add();
}
public void run(){
for (int i = 0; i < 1000; i++) {
add();
}
}
}
class PRemove extends Thread{
private Box box;
public PRemove(Box box) {
this.box = box;
}
public void remove() {
box.remove();
}
public void run() {
for(int i=0; i<1000; i++){
remove();
}
}
}
运行结果:
盒子中没有产品,等待添加!
盒子中没有产品,等待添加!
当前容量:1 --->Thread-0
当前容量:2 --->Thread-0
当前容量:3 --->Thread-0
当前容量:4 --->Thread-0
当前容量:5 --->Thread-0
当前容量:6 --->Thread-0
当前容量:7 --->Thread-0
当前容量:8 --->Thread-0
当前容量:9 --->Thread-0
当前容量:10 --->Thread-0
盒子已满,等待删除!
当前容量:9 --->Thread-3
当前容量:8 --->Thread-3
当前容量:7 --->Thread-3
当前容量:6 --->Thread-3
当前容量:5 --->Thread-3
当前容量:4 --->Thread-3
当前容量:3 --->Thread-3
当前容量:2 --->Thread-3
当前容量:1 --->Thread-3
当前容量:0 --->Thread-3
盒子中没有产品,等待添加!
盒子中没有产品,等待添加!
*
*/
import java.util.*;
public class PCModel {
public static void main(String[] args) {
Box box = new Box();
PAdd p1 = new PAdd(box);
PRemove p2 = new PRemove(box);
PAdd p3= new PAdd(box);
PRemove p4 = new PRemove(box);
PAdd p5 = new PAdd(box);
PRemove p6 = new PRemove(box);
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
}
}
class Box {
private static final int max = 10;
private List<Integer> list;
public Box(){
list = new ArrayList<>();
}
public void add() {
synchronized (list) {
while(list.size() >= max) {
System.out.println("盒子已满,等待删除!");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(1);
System.out.println("当前容量:" + list.size() + " --->" + Thread.currentThread().getName());
list.notifyAll();
}
}
public void remove() {
synchronized (list) {
while(list.size()==0){
System.out.println("盒子中没有产品,等待添加!");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.remove(0);
System.out.println("当前容量:" + list.size() + " --->" + Thread.currentThread().getName());
list.notifyAll();
}
}
}
class PAdd extends Thread{
private Box box;
public PAdd(Box box) {
this.box = box;
}
public void add() {
box.add();
}
public void run(){
for (int i = 0; i < 1000; i++) {
add();
}
}
}
class PRemove extends Thread{
private Box box;
public PRemove(Box box) {
this.box = box;
}
public void remove() {
box.remove();
}
public void run() {
for(int i=0; i<1000; i++){
remove();
}
}
}
运行结果:
盒子中没有产品,等待添加!
盒子中没有产品,等待添加!
当前容量:1 --->Thread-0
当前容量:2 --->Thread-0
当前容量:3 --->Thread-0
当前容量:4 --->Thread-0
当前容量:5 --->Thread-0
当前容量:6 --->Thread-0
当前容量:7 --->Thread-0
当前容量:8 --->Thread-0
当前容量:9 --->Thread-0
当前容量:10 --->Thread-0
盒子已满,等待删除!
当前容量:9 --->Thread-3
当前容量:8 --->Thread-3
当前容量:7 --->Thread-3
当前容量:6 --->Thread-3
当前容量:5 --->Thread-3
当前容量:4 --->Thread-3
当前容量:3 --->Thread-3
当前容量:2 --->Thread-3
当前容量:1 --->Thread-3
当前容量:0 --->Thread-3
盒子中没有产品,等待添加!
盒子中没有产品,等待添加!
相关文章推荐
- IDEA配置jdk
- Eclipse项目导入Android Studio错误积累
- Eclipse 的 Debug 介绍与技巧
- 2016.03.04(struts前台取值)
- java调用百度定位api服务获取地理位置示例
- Java生成PDF报表
- 了解java的序列化与反序列化
- java Serializable和Externalizable序列化反序列化详解
- spring拦截器引起的问题
- String类中的IndexOf和CharAt的区别
- springMVC+hibernate+jdbc+mybatis整合 支持全注解方式 支持全配置方式
- try catch finally throw throws异常捕获和抛出demo
- 2016.03.04///Java学习记录①
- java web知识结构
- MyEclipse如何调试
- Java Calendar 类的时间操作
- Struts2基础(一个简单的小例子)
- JSON与JAVA数据的转换
- DB2triples的使用
- Xamarin.Forms教程下载安装JDK配置环境变量