Producers and consumers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.