Package Hpu.acm.lzl.demos;
/**
* Producer and consumer issues
* Problem Description: A basket container to put steamed bread, producers responsible for the production of steamed bread, into the basket, the consumer is responsible for taking steamed bread from the basket to eat.
* @author Lzl
* Problem Analysis:
* Inside a Basket Container class Syncstack provides push methods for producer production, pop classes for consumer consumption
* Steamed Bread Class Streamedbun
* Production of this type of Producer
* Consumer class Consumer
*/
public class Producerconsumer {
public static void Main (string[] args) {
Syncstack ss = new Syncstack ();
Producer p = new Producer (ss);
Consumer C = new Consumer (ss);
New Thread (P). Start (); Producers
New Thread (P). Start (); Producers
New Thread (P). Start (); Producers
New Thread (P). Start (); Producers
New Thread (P). Start (); Producers
New Thread (c). Start (); Consumers
}
}
Define the steamed bun class and provide its ID number
Class streamedbun{
int id;
public Streamedbun (int id) {
This.id = ID;
}
Public String toString () {
Return "Steamed bread:" +id;
}
}
Define a container to put steamed bread
Class syncstack{
Streamedbun []MT = new STREAMEDBUN[10]; Only 10 steamed buns can be loaded into the definition container
int index=0; Add an index to find
To accumulate the steamed bread, synchronized keyword is used to lock the current method and prevent other threads from interrupting
Public synchronized void push (Streamedbun sb) {
while (index = = mt.length) {
try {
This.wait (); Here if the basket of steamed bread filled, it is necessary to wait for consumers to consume some, in order to reproduce.
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
This.notify (); The producer will be awakened to produce steamed bread.
Mt[index] = SB; Add steamed bread to the container
index++;
}
The method of removing the steamed bread from the basket.
Public synchronized Streamedbun Pop () {
while (index = = 0) {
try {
This.wait (); Consumers to consider, if the basket in the empty, then wait for the producers, production of steamed bread.
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
This.notify (); Will wake the consumer, eat steamed bread.
Index--;
return Mt[index];
}
}
Producers, only need to call the basket container's push method, it can be produced.
Class Producer implements runnable{
Public Syncstack SS;
Public Producer (Syncstack ss) {
THIS.SS = SS;
}
@Override
public void Run () {
for (int i=0;i<30;i++) {//Assuming a cycle produces 30 steamed buns
Streamedbun sb = new Streamedbun (i);
Ss.push (SB);
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Producer: Produced" +SB);
}
}
}
Consumers, only need to call the Basket container pop method, can be consumed.
Class Consumer implements runnable{
Public Syncstack SS;
Public Consumer (Syncstack ss) {
THIS.SS = SS;
}
@Override
public void Run () {
for (int i =0;i<30;i++) {
Streamedbun sb = Ss.pop ();
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Consumer: Consumption" +SB);
}
}
}
Examples of thread producer consumers