BlockingQueue has four conditions: throwing an exception, returning special values, blocking, and timeout. The following table summarizes these methods:
Throw exception special value blocking timeout
Insert add (e) offer (e) put (e) offer (e, time, unit)
Remove () poll () take () poll (time, unit)
Check that element () peek () is unavailable
BlockingQueue is an interface and has the following implementation classes:
1. ArrayBlockQueue: A Bounded blocking queue supported by arrays. This queue sorts the elements according to the FIFO principle. The size of the object to be created must be clear, just like an array.
2. Define blockqueue: a blocking queue that can change the size. This queue sorts the elements according to the FIFO principle. If the size of the created object is not specified, the default value is Integer. MAX_VALUE. The throughput of the link queue is usually higher than that of the array-Based Queue, but its predictable performance is lower in most concurrent applications.
3. PriorityBlockingQueue: similar to LinkedBlockingQueue. However, the sorting of objects contained in PriorityBlockingQueue is determined based on the natural sorting order of objects or the Comparator in the constructor.
4. SynchronousQueue: Synchronous queue. The synchronization queue does not have any capacity. Each insert must wait for another thread to be removed, and vice versa.
The following uses ArrayBlockQueue to implement the producer consumer/payer mode previously implemented. The Code is as follows:
[Java]
/** Define a plate class that can hold eggs and take eggs */
Public class BigPlate {
/** Plate with eggs. The size is 5 */
Private BlockingQueue <Object> eggs = new ArrayBlockingQueue <Object> (5 );
/** Put eggs */
Public void putEgg (Object egg ){
Try {
Eggs. put (egg); // put an egg at the end of the plate. If the plate is full, the current thread is blocked.
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("put eggs ");
}
/** Get eggs */
Public Object getEgg (){
Object egg = null;
Try {
Egg = eggs. take (); // get an egg from the plate. If the plate is empty, the current thread is blocked.
} Catch (InterruptedException e ){
E. printStackTrace ();
}
System. out. println ("get eggs ");
Return egg;
}
/** Put the eggs on the thread */
Static class AddThread extends Thread {
Private BigPlate;
Private Object egg = new Object ();
Public AddThread (BigPlate plate ){
This. plate = plate;
}
Public void run (){
Plate. putEgg (egg );
}
}
/** Get the egg thread */
Static class GetThread extends Thread {
Private BigPlate;
Public GetThread (BigPlate plate ){
This. plate = plate;
}
Public void run (){
Plate. getEgg ();
}
}
Public static void main (String [] args ){
BigPlate plate = new BigPlate ();
// Start 10 put eggs threads first
For (int I = 0; I <10; I ++ ){
New Thread (new AddThread (plate). start ();
}
// Start 10 more egg fetch threads
For (int I = 0; I <10; I ++ ){
New Thread (new GetThread (plate). start ();
}
}
}
Execution result:
[Plain]
Put eggs
Put eggs
Put eggs
Put eggs
Put eggs
Get the eggs
Put eggs
Get the eggs
Get the eggs
Get the eggs
Put eggs
Put eggs
Put eggs
Get the eggs
Put eggs
Get the eggs
Get the eggs
Get the eggs
Get the eggs
Get the eggs
From the results, we started 10 put eggs threads and 10 get eggs threads. Five put eggs into five threads were successfully executed, to 6th, and we found that the plate was full and blocked, switch to the egg fetch thread for execution, and the producer/consumer mode is successfully implemented.