模拟公司仓库、生产、销售的过程
package cn.thread;
/**
* 公司
* @author 姚伟楠
*
*/
public class Company {
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Company com=new Company();
com.produte();
com.sale();
}
private Storage storage=new Storage();//公司仓库
private int producer_num=5;//公司生产线人数
private int saler_num=3;//公司销售人数
private int produce_internal=2*1000;//每间隔2秒一个人可以生产一个产品
/**
* 生产
*/
public void produte(){
// TODO 找一定数量人员开始生产产品
for(int i=0;i<producer_num;i++){
new Producer((long) i,"No."+i+"Pro") {
public void producte() {
synchronized(Producer.class){//防止编号大的产品比编号小的产品早放在仓库
while(true){
try {
Thread.currentThread().sleep(produce_internal);
} catch (InterruptedException e) {
e.printStackTrace();
}
Good good=new Good();
synchronized (storage) {
while (!(storage.getNumOfGood()<storage.getStorage_size())) {
//仓库已满,进入等待
try {
storage.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storage.putGood(good);
System.out.println(this.getName()+"生产"+good.getXh()+"号产品,仓库剩余"+storage.getNumOfGood()+"物品");
storage.notify();
}
}
}
}
}.start();
}
}
public void sale(){
// TODO 找一定数量的人员来销售产品
for(int i=0;i<saler_num;i++){
new Saler((long) i,"No."+i+"Saler") {
public void sale() {
while(true){
try {
Thread.currentThread().sleep((long)(10000* Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (storage) {
while (storage.getNumOfGood()<1) {//仓库无货物,进入等待
try {
storage.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Good good=storage.getGood();
System.out.println(this.getName()+"销售"+good.getXh()+"号产品,仓库剩余"+storage.getNumOfGood()+"物品");
storage.notify();
}
}
}
}.start();
}
}
}
仓库类
package cn.thread;
import java.util.Stack;
/**
* 仓库类
*
* @author 姚伟楠
*
*/
public class Storage {
private final Stack<Good> store = new Stack<Good>();
private final int storage_size=100;
/**
* 放入货物
*
* @param good
*/
public void putGood(Good good) {
store.push(good);
}
/**
* 拿出货品
*
* @return
*/
public Good getGood() {
Good good=store.pop();
return good;
}
/**
* 获得仓库中货品数量
* @return
*/
public int getNumOfGood(){
return store.size();
}
/**
* 获得仓库大小
* @return
*/
public int getStorage_size() {
return storage_size;
}
}
生产者类
package cn.thread;
/**
* 生产者,生产者生产对象不明确,定义为抽象类,具有抽象的生产方法
*
* @author 姚伟楠
*
*/
public abstract class Producer implements Runnable {
private Long id;// 生产者编号
private String name;// 生产者名称
private Thread thread=new Thread(this);//单独生产
public Producer(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 生产物品
*/
public abstract void producte();
@Override
public void run() {
producte();
}
/**
* 开始生产
*/
public void start(){
thread.start();
}
/**
* 停止生产
*/
public void stop(){
thread.stop();
}
}
销售者类
package cn.thread;
/**
* 销售员,由于未知此售货员销售的是哪个仓库的货物,所以此处销售对象不明确,定义为抽象方法
* @author 姚伟楠
*
*/
public abstract class Saler implements Runnable{
private Long id; //售货员编号
private String name;//名称
private Thread thread=new Thread(this);//单独销售
/**
* 销售物品
*/
public abstract void sale();
public Saler(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void run() {
sale();
}
/**
* 开始销售
*/
public void start(){
thread.start();
}
/**
* 停止销售
*/
public void stop(){
thread.stop();
}
}
物品类
package cn.thread;
/**
* 物品类
* @author 姚伟楠
*
*/
public class Good {
public static Long xh_icreate=0L;
private String xh;//物品流水号
public Good() {
xh=this.format(++xh_icreate);
}
/**
* 格式化产品编号为16位数字
* @param long1
* @return
*/
private String format(Long long1) {
String tmp=long1+"";
int length=tmp.length();
while(18-length>0){
tmp="0"+tmp;
length=tmp.length();
}
return tmp;
}
public String getXh() {
return xh;
}
}