package com.hjx.product_customer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* 运行测试类
* @author hjx
*
*/
public class MainTest {
public static void main(String[] args) {
//存储所有线程的Map
Map<String,Runnable> runnabelMap =new HashMap<String,Runnable>();
//仓库类
Storage storage =new Storage();
//这里使用带缓存功能的线程池
ExecutorService sercice = Executors.newCachedThreadPool();
Productor p1 = null;
Customer c1 = null;
String name ="";
//循环创建各100个生产者线程和消费者线程
for (int i = 0; i < 100; i++) {
name =i +"-p";
p1 = new Productor(true,storage,name);
runnabelMap.put(name, p1);
name =i + "-m";
c1 = new Customer(true,storage,name);
runnabelMap.put(name, c1);
sercice.submit(p1);
sercice.submit(c1);
}
try {
//休眠10秒后
TimeUnit.SECONDS.sleep(10);
//开始停止所有的线程
Set set = runnabelMap.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String keyName = (String ) it.next();
BaseRunnable r = (BaseRunnable) runnabelMap.get(keyName);
r.setGoing(false); //设置线程停止的标识
}
//休眠10秒后
TimeUnit.SECONDS.sleep(10);
//查看仓库里还有多少产品未消费。
int count = storage.getQueue().size();
System.out.println("仓库里未消费的产品数量: " + count);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main is end!");
}
}
package com.hjx.product_customer;
import java.util.concurrent.TimeUnit;
/**
* 生产者
* @author hjx
*
*/
public class Productor extends BaseRunnable {
private Storage storage;
private String name;
public Productor(boolean isGoing,Storage storage,String name) {
this.setGoing(isGoing);
this.storage = storage;
this.name = name;
}
@Override
public void run() {
while (this.isGoing()) {
String pName ="产品" + (int)(Math.random() * 10000);
ProductInfo productInfo =new ProductInfo(pName);
try {
storage.push(productInfo);
System.out.println(name +" 生产产品: "+ productInfo.toString());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产者已经停止生产!");
}
}
package com.hjx.product_customer;
import java.util.concurrent.TimeUnit;
/**
* 消费者
* @author hjx
*
*/
public class Customer extends BaseRunnable {
private Storage storage;
private String name;
public Customer(boolean isGoing,Storage storage,String name) {
this.setGoing(isGoing);
this.name = name;
this.storage = storage;
}
@Override
public void run() {
while (this.isGoing()) {
try {
ProductInfo p = storage.pop();
System.out.println(name +" 消费产品: " +p.toString());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者已经停止消费!");
}
}
package com.hjx.product_customer;
/**
* 产品对象
* @author hjx
*
*/
public class ProductInfo {
private String name;
public ProductInfo(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ProductInfo [name=" + name + "]";
}
}
package com.hjx.product_customer;
/**
* 生产者、消费者的父类
* @author hjx
*
*/
public class BaseRunnable implements Runnable {
private boolean isGoing;//生产者、消费者是否需要继续生产或者消费的条件
public boolean isGoing() {
return isGoing;
}
public void setGoing(boolean isGoing) {
this.isGoing = isGoing;
}
@Override
public void run() {
System.out.println("父类方法");
}
}
运行效果: