JAVA多线程编程之批量生产者消费者问题实例介绍



JAVA多线程编程之批量生产者消费者问题实例介绍。最近在找工作,自学已经遗忘的多线程知识,就在网上查看了经典的生产者消费者例子,发现都是一次性生产和消费一个的,于是我就想搞一个多生产多消费者的例子。写在博客中全当是记录自己的学习记录,若纰漏百出,望不吝指出,谢谢!
实体bean类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
package thread.productAndConsumer;

public class ProductsVO {

private String name ;
private String id ;

public ProductsVO(String name ,String id){
this.name = name;
this.id = id;
}

public String toString(){
return “产品名称:”+name+” 产品编码:”+id;
}
}

数据仓库类(资源共享):

这个数据仓库是参照别人,设计成了仓库栈,每次生产和消费都是在栈顶操作FILO

[java] view plaincopy在CODE上查看代码片派生到我的代码片
package thread.productAndConsumer;

/**
*
* @author Kimi 批量生产者和消费者 仓库
*/
public class StorageBulk {
// 仓库容量为100
private ProductsVO[] products = new ProductsVO[100];
private int top = -1;

public synchronized void pop(int nums) {
int loopTimes = 0;
ProductsVO returnPro = null;
while (loopTimes != nums) {
while (top != -1 && loopTimes != nums) {
returnPro = products[top];
products[top] = null;
top–;
loopTimes++;
System.out.println(Thread.currentThread().getName() + “消费了” + returnPro
+ ” 剩余羊肉串:” + String.valueOf(top + 1));
this.notifyAll();
}
while (top == -1) {
try {
this.wait();
} catch (InterruptedException e) {

}
}
}
}

public synchronized void push(ProductsVO[] productArray) {
int loopTimes = 0;
while (loopTimes != productArray.length) {
while (top != products.length – 1
&& loopTimes != productArray.length) {
products[++top] = productArray[loopTimes++];
System.out.println(Thread.currentThread().getName() + ” 生产了”
+ productArray[loopTimes - 1] + ” 剩余羊肉串:”
+ String.valueOf(top + 1));
this.notifyAll();
}
System.out.println(Thread.currentThread().getName()
+ “*************” + “*********批量生产了********” + loopTimes
+ “串羊肉串, ” + ” 有:” + (productArray.length – loopTimes)
+ “未烤好 ” + “库存有” + (top + 1) + “串羊肉串烤好未被购买”);
while (top == products.length – 1) {
try {
this.wait();
} catch (InterruptedException e) {


}
}
}
}
}

生产者:生产20个保存在数组里
[java] view plaincopy在CODE上查看代码片派生到我的代码片
package thread.productAndConsumer;
/**
*
* @author Kimi
*
*/
public class ProductBulk implements Runnable {

private StorageBulk storageBulk;

public ProductBulk(StorageBulk storageBulk){
this.storageBulk = storageBulk ;
}

@Override
public void run() {
// TODO Auto-generated method stub
ProductsVO[] productArray = new ProductsVO[20];
for(int i=0;i<productArray.length;i++){
productArray[i] = new ProductsVO(“羊肉串”, String.valueOf(Math.random()*100000));
}
storageBulk.push(productArray);
}

}

消费者:消费者一次消费15个
[java] view plaincopy在CODE上查看代码片派生到我的代码片
package thread.productAndConsumer;

public class CosumerBulk implements Runnable{
private StorageBulk storageBulk;

public CosumerBulk(StorageBulk storageBulk) {
this.storageBulk = storageBulk;
}

@Override
public void run() {
// TODO Auto-generated method stub
storageBulk.pop(15);
}
}

测试类:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
package thread.productAndConsumer;

public class TestBulk {
public static void main(String[] args){
StorageBulk storage = new StorageBulk();
for(int i=0;i<10;i++){
new Thread(new ProductBulk(storage),”生产者”).start();
new Thread(new CosumerBulk(storage),”消费者”).start();
}
}
}
生产的羊肉串大于消费的羊肉串,线程结束