50,550
社区成员
发帖
与我相关
我的任务
分享
public class FactoryStock
{
public String message="";
public boolean hasStock = false;
public synchronized String getProduct()
{
while(!hasStock)
{
try
{
System.out.println("Waiting in consumer");
wait();
Thread.sleep(2000);
}
catch(InterruptedException e)
{
}
}
hasStock = false;
//notifyAll();
return this.message;
}
public synchronized void putProduct(String productName)
{
while(hasStock)
{
try
{
System.out.println("Waiting in producer "+productName);
wait();
Thread.sleep(2000);
}
catch(InterruptedException e)
{
}
}
hasStock = true;
this.message = "This is product:"+productName;
notifyAll();
}
}
public class ProducerThread implements Runnable
{
private FactoryStock stock;
private String producerName;
public ProducerThread(String producerName,FactoryStock stock)
{
this.stock = stock;
this.producerName = producerName;
}
public void run()
{
// TODO Auto-generated method stub
int i=1;
while(true)
{
stock.putProduct("Product name:"+producerName+"["+i+++"]");
System.out.println("Producer "+producerName+" put product:"+i);
}
}
}
class consumerThread implements Runnable
{
private FactoryStock stock;
private String consumerName;
public consumerThread(String consumerName,FactoryStock stock)
{
this.stock = stock;
this.consumerName=consumerName;
}
public void run()
{
while(true)
{
System.out.println("Consumer "+consumerName+" get product:"+stock.getProduct());
}
}
}
public class TestFactory
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
FactoryStock stock = new FactoryStock();
Thread consumer1 = new Thread(new consumerThread("Consumer 1",stock));
Thread consumer2 = new Thread(new consumerThread("Consumer 2",stock));
Thread consumer3 = new Thread(new consumerThread("Consumer 3",stock));
Thread consumer4 = new Thread(new consumerThread("Consumer 4",stock));
Thread consumer5 = new Thread(new consumerThread("Consumer 5",stock));
Thread consumer6 = new Thread(new consumerThread("Consumer 6",stock));
Thread consumer7 = new Thread(new consumerThread("Consumer 7",stock));
Thread consumer8 = new Thread(new consumerThread("Consumer 8",stock));
Thread consumer9 = new Thread(new consumerThread("Consumer 9",stock));
Thread consumer10 = new Thread(new consumerThread("Consumer 10",stock));
Thread producer1 = new Thread(new ProducerThread("Producer 1",stock));
Thread producer2 = new Thread(new ProducerThread("Producer 2",stock));
Thread producer3 = new Thread(new ProducerThread("Producer 3",stock));
Thread producer4 = new Thread(new ProducerThread("Producer 4",stock));
Thread producer5 = new Thread(new ProducerThread("Producer 5",stock));
Thread producer6 = new Thread(new ProducerThread("Producer 6",stock));
Thread producer7 = new Thread(new ProducerThread("Producer 7",stock));
Thread producer8 = new Thread(new ProducerThread("Producer 8",stock));
Thread producer9 = new Thread(new ProducerThread("Producer 9",stock));
Thread producer10 = new Thread(new ProducerThread("Producer 10",stock));
Thread producer11 = new Thread(new ProducerThread("Producer 11",stock));
Thread producer12 = new Thread(new ProducerThread("Producer 12",stock));
Thread producer13 = new Thread(new ProducerThread("Producer 13",stock));
Thread producer14 = new Thread(new ProducerThread("Producer 14",stock));
Thread producer15 = new Thread(new ProducerThread("Producer 15",stock));
Thread producer16 = new Thread(new ProducerThread("Producer 16",stock));
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
consumer5.start();
consumer6.start();
consumer7.start();
consumer8.start();
consumer9.start();
consumer10.start();
producer1.start();
producer2.start();
producer3.start();
producer4.start();
producer5.start();
producer6.start();
producer7.start();
producer8.start();
producer9.start();
producer10.start();
producer11.start();
producer12.start();
producer13.start();
producer14.start();
producer15.start();
producer16.start();
}
}