Package javastudy;
public class Consumersandprodusers {
/**
* Test method
* @param args
*/
public static void Main (string[] args) {
Build the Data warehouse and instantiate it.
Storage Storage = new Storage ();
Creating Consumer Threads
Thread consumerthread = new Thread (new consumers (storage));
Consumerthread.setname ("@ Consumer thread @");
Build a producer Thread
Thread producerthread = new Thread (new producers (storage));
Producerthread.setname ("@ Production line @");
Start line and consumer threads
Consumerthread.start ();
Producerthread.start ();
}
}
/**
* Consumer Class
* @author AB049399
*
*/
Class consumers implements runnable{
Private Storage Storage;
Public consumers (Storage Storage) {
This.storage = storage;
}
public void Run () {
while (true) {
Storage.pop ();
}
}
}
/**
* Producer Class
* @author AB049399
*
*/
Class producers implements runnable{
Private Storage Storage;
Public producers (Storage Storage) {
This.storage = storage;
}
public void Run () {
Product Product = new Product ("@@ Product ID", "Product name", "Product other information");
while (true) {
Storage.push (product);
}
}
}
/**
* Product Category
* @author AB049399
*
*/
Class product{
String ID;
String name;
String others;
Public Product (String id,string name,string others) {
This.id = ID;
THIS.name = name;
This.others = others;
}
Public String toString () {
Return "(Product id=" +id+ ", Product name =" +name+ ", other attribute =" +others+ ")";
}
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String getothers () {
return others;
}
public void Setothers (String others) {
This.others = others;
}
}
/**
* Data Warehouse Class (Stack mode)
* @author AB049399
*
*/
Class storage{
Private final int storage_sum=10;//assumes a capacity of 10
Private product[] Products = new Product[storage_sum]; Product Warehouse
private int top = 0;//at the top of the current warehouse
Production Products
Public synchronized void push (product product) {
while (top = = storage_sum) {//warehouse capacity is full, no more production, wait
try {
System.out.println ("Current warehouse is full, thread" +thread.currentthread (). GetName () + "Waiting!" ");
Wait ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
Put the product into the warehouse
products[top++] = product;
System.out.println (Thread.CurrentThread (). GetName () + "Production of new products, product information:" +product.tostring ());
Notifyall ();//Wake Up all waiting threads
}
Consumer Products
Public synchronized Product Pop () {
while (top = = 0) {//the product in the warehouse is empty, waiting for
try {
System.out.println ("Current warehouse has no product, thread" +thread.currentthread (). GetName () + "Waiting!" ");
Wait ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Consumer Products
Product Product = Products[--top];
Product Product = new Product (Products[--top].getid (), Products[top].getname (), products[top].getothers ());
Products[top] = null;
System.out.println (Thread.CurrentThread (). GetName () + "consumer products, product information:" +product.tostring ());
Notifyall ();//Wake Up all waiting threads
return product;
}
}
Producers and consumers