Use Condition to implement multi-threaded calls (production consumption mode) and conditional multi-thread
1. wait (), Policy (), and policyall () of the object class ()
The Java Thread class is also an object class, and its instances all inherit from java. lang. Thread or its subclass.
Wait (), Y (), and notifyAll () are methods in the Object class and are often used for scheduling between threads.
When threads run without data, you can call wait () to wait for threads without occupying CUP resources and improve the CPU utilization.
For example, thread B can wait for A signal from thread A, which notifies thread B that data has been prepared and B can execute business logic.
Scheduling between threads is often used in the production and consumption modes.
You can use the wait (), Policy (), and policyall () of the Object, but JDK provides a better alternative.
2. Condition class enhancement class
Condition class, implementing collaboration between threads. Compared with the wait () and Policy () of objects, the conditional await () and signal () in this way, Inter-thread collaboration is more secure and efficient. Therefore, Condition is generally recommended.
Condition depends on the Lock interface. The basic code for generating a Condition is lock. newCondition ()
The await () and signal () Methods of Condition can be used only when they are between lock. lock () and lock. unlock.
Iii. Comparison between objects and Conditon
Await () in Conditon corresponds to the wait () of the Object ();
The signal () in Condition corresponds to the notify () of the Object ();
SignalAll () in Condition corresponds to policyall () of the Object ().
4. Use Condition for production and consumption
Import java. util. concurrent. locks. condition; import java. util. concurrent. locks. lock; import java. util. concurrent. locks. reentrantLock;/*** Created by tank on 2016/11/22. * use Condition to implement the production and consumption mode between threads */public class ConditionDemo {Object [] queue = new Object [100]; // queue int readIndex = 0; // read index location int writeIndex = 0; int dataLen = 0; final Lock lock = new ReentrantLock (); final Condition fullCondition = l Ock. newCondition (); final Condition emptyCondition = lock. newCondition (); public static void main (String [] args) {final ConditionDemo demo = new ConditionDemo (); new Thread () {@ Override public void run () {for (int I = 0; I <1000; I ++) {demo. write (I );}}}. start (); new Thread () {@ Override public void run () {while (true) {Object obj = demo. read (); if (obj! = Null) {System. out. println (Integer) obj );}}}}. start () ;}// produce public void write (Object obj) {lock. lock (); try {if (dataLen> = queue. length) {// The queue is full of System. out. println ("the queue is full, wait ..... "); fullCondition. await (); System. out. println ("the queue is empty. Wake up ..... ");} queue [writeIndex] = obj; writeIndex ++; dataLen ++; if (writeIndex> = queue. length) {writeIndex = 0;} emptyCondition. signal ();} catch (InterruptedException e) {e. printStackTrace ();} finally {lock. unlock () ;}// consume public Object read () {lock. lock (); try {if (dataLen <= 0) {System. out. println ("the queue is empty, waiting for data ..... "); emptyCondition. await (); System. out. println ("the queue has data. Wake up ..... ");} Object obj = queue [readIndex]; readIndex ++; dataLen --; if (readIndex> = queue. length) {readIndex = 0;} fullCondition. signal (); return obj;} catch (InterruptedException e) {e. printStackTrace ();} finally {lock. unlock () ;}return null ;}}