From: http://lavasoft.blog.51cto.com/62575/221932
Java thread: Concurrent collaboration-producer consumer model is the most classic model for multi-threaded programs, regardless of any programming language. Just like learning every programming language, Hello World! Are the most classic examples. In fact, it should be accurate to say that it is the "producer-consumer-warehouse" model. After leaving the warehouse, the producer-consumer model is unconvincing. For this model, the following points should be clarified: 1. The producer only stops production when the Warehouse is full and the warehouse is full. 2. Consumers can only consume data when there are products in the warehouse, while empty warehouses are waiting. 3. When a consumer finds that there is no product in the warehouse to be consumed, the producer will be notified of production. 4. When the producer generates a consumable product, it should notify the waiting consumer to consume it. This model will be combined with the wait, y, and yyall methods of java. lang. Object to achieve the above requirements. This is very important. /**
* Java thread: Concurrent collaboration-producer consumer model
*
* @ Author leizhimin 2009-11-4 14:54:36
*/
Public class Test {
Public static void main (String [] args ){
Godown godown = new Godown (30 );
Consumer c1 = new Consumer (50, godown );
Consumer c2 = new Consumer (20, godown );
Consumer c3 = new Consumer (30, godown );
Producer p1 = new Producer (10, godown );
Producer p2 = new Producer (10, godown );
Producer p3 = new Producer (10, godown );
Producer p4 = new Producer (10, godown );
Producer p5 = new Producer (10, godown );
Producer p6 = new Producer (10, godown );
Producer p7 = new Producer (80, godown );
C1.start ();
C2.start ();
C3.start ();
P1.start ();
P2.start ();
P3.start ();
P4.start ();
P5.start ();
P6.start ();
P7.start ();
}
}
/**
* Repository
*/
Class Godown {
Public static final int max_size
= 100; // maximum inventory
Public int curnum; // current inventory
Godown (){
}
Godown (int curnum ){
This. curnum = curnum;
}
/**
* Produce a specified number of products
*
* @ Param neednum
*/
Public synchronized void produce (int neednum)
{
// Test whether production is required
While (neednum + curnum> max_size ){
System. out. println ("number of products to be produced" + neednum + "excess inventory" + (max_size-curnum) + ", production tasks cannot be executed for the moment! ");
Try {
// The current production thread waits
Wait ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
// If the production conditions are met, the current inventory is changed.
Curnum + = neednum;
System. Out. println ("has been produced" + neednum + "products, the current storage volume is" + curnum );
// Wake up all threads waiting on this object Monitor
Policyall ();
}
/**
* Consume a specified number of products
*
* @ Param neednum
*/
Public synchronized void consume (int neednum)
{
// Test availability
While (curnum <neednum ){
Try {
// The current production thread waits
Wait ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
// If the consumption condition is met, the current inventory is changed.
Curnum-= neednum;
System. Out. println ("consumed" + neednum + "products, the current storage volume is" + curnum );
// Wake up all threads waiting on this object Monitor
Policyall ();
}
}
/**
* Producer
*/
Class Producer extends Thread {
Private int neednum; // number of production products
Private Godown godown; // Repository
Producer (int neednum, Godown godown ){
This. neednum = neednum;
This. godown = godown;
}
Public void run (){
// Produce a specified number of products
Godown. produce (neednum );
}
}
/**
* Consumer
*/
Class Consumer extends Thread {
Private int neednum; // number of production products
Private Godown godown; // Repository
Consumer (int neednum, Godown godown ){
This. neednum = neednum;
This. godown = godown;
}
Public void run (){
// Consume a specified number of products
Godown. Consume (neednum );
}
} 10 products have been produced, and the current storage volume is 40
10 products have been produced, and the current storage volume is 50
50 products have been consumed, and the current storage volume is 0
80 products have been produced, and the current storage volume is 80
30 products have been consumed, and the current storage volume is 50
10 products have been produced, and the current storage volume is 60
20 products have been consumed, and the current storage volume is 40
10 products have been produced, and the current storage volume is 50
10 products have been produced, and the current storage volume is 60
10 products have been produced, and the current storage volume is 70
Process finished with exit code 0
Note: In this example, the wait method of the object is called when the production or consumption conditions cannot be met, the wait method is used to release the lock obtained by the current thread, call the notifyAll () method of the object, and notify (wake up) Other waiting threads on the object so that it can continue execution. In this way, the entire producer and consumer thread can be correctly executed collaboratively. The yyall () method serves as a notification, neither releasing or obtaining a lock. I just told the waiting thread on this object that "the thread can compete for execution, and I will wake up to execute it ". This example is only the simplest expression in the producer-consumer model. In this example, if the storage volume consumed by a consumer does not meet the requirement and no producer exists, the program remains in the waiting state, of course, this is not true. In fact, this example can be modified to produce based on consumption, while both the production and the warehouse are taken into account. If the warehouse is not satisfied, the production will be produced, and the maximum consumption will be limited each time, in this way, this problem does not exist. Of course, such an example is more complex and it is more difficult to describe such a simple model. I like simple examples.
This article is from the "melyan" blog, please be sure to keep this source http://lavasoft.blog.51cto.com/62575/221932