package com.gcoresoft.homework.consumerproducer;


public class Test
{


/** *//**
* @param args
*/

public static void main(String[] args)
{
// TODO Auto-generated method stub
Products pros=new Products();
Producers[] producers=new Producers[5];
for(int i=0;i<producers.length;i++)

{
producers[i]=new Producers(pros, i, 5);
}
Consumers[] consumers=new Consumers[2];
for(int i=0;i<consumers.length;i++)

{
consumers[i]=new Consumers(pros, i);
}
for(int i=0;i<producers.length;i++)

{
producers[i].start();
}
for(int i=0;i<consumers.length;i++)

{
consumers[i].start();
}
}

}

消费者
package com.gcoresoft.homework.consumerproducer;


public class Consumers extends Thread
{

private final int ID;
private Products pros;
public Consumers(Products pros, int ID)

{
this.ID=ID;
setProducts(pros);
}
public void setProducts(Products pros)

{
this.pros=pros;
}
public void run()

{
for(int i=0;i<100;i++)

{
System.out.println("Consumer "+ ID +"消费了"+pros.get());
try

{
sleep((int)(Math.random()*2000));

}catch(Exception e)
{
//e.printStackTrace();
System.out.println("Exception in Consumer "+ID);
}
}
}
}

生产者
package com.gcoresoft.homework.consumerproducer;


public class Producers extends Thread
{

private Products pros;
private final int ID;
private int num=0;
public Producers(Products pros, int ID, int num)

{
this.ID=ID;
setProductsNum(num);
setProducts(pros);
}
public void setProducts(Products pros)

{
this.pros=pros;
}

public void setProductsNum(int num)

{
this.num=num;
}
public void run()

{
for(int i=0;i<num;i++)

{
pros.add(ID*10+i);
System.out.println("Producer "+ ID +"生产了"+(ID*10+i));
try

{
sleep((int)(Math.random()*2000));

}catch(Exception e)
{
//e.printStackTrace();
System.out.println("Exception in Producer "+ID);
}
}
}
}

产品
package com.gcoresoft.homework.consumerproducer;


public class Products
{

private static int count;
private static int num;
private static int[] products;

/** *//**
* @param args
*/

public static void main(String[] args)
{
// TODO Auto-generated method stub
Products pros=new Products(200);
for(int i=0;i<1000;i++)
pros.add(i);
for(int i=0;i<200;i++)
System.out.println("get:"+pros.get());
}
public Products()

{
setMaxNum(100);
}
public Products(int num)

{
setMaxNum(num);
}
public void setMaxNum(int num)

{
this.num=num;
products=new int[num];
count=0;
}

public synchronized void add(int i)

{
while(isFull())

try
{
wait();

} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
products[count++]=i;
//System.out.println("count:"+(count-1));
notify();
}
private boolean isFull()

{
return count==num;
}
public synchronized int get()

{
while(isEmpty())

try
{
wait();

} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
notify();
//System.out.println("count:"+count);
return products[--count];
}
private boolean isEmpty()

{
return count==0;
}

}

测试
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2007-04-12 14:51
TiGERTiAN 阅读(910)
评论(0) 编辑 收藏 所属分类:
Java