015.2Condiction interface,
The Condiction object can let the thread wait and wake up the corresponding thread. For details, refer to the code below:
Await ();
Signal ();
SignalAll ();
Procedure:
1) create a lock
2) Get the Condiction object through the lock
3) after the Condiction object waits, someone else needs to wake up to continue executing the process.
Sample Code:
1 import java. util. concurrent. locks. condition; 2 import java. util. concurrent. locks. lock; 3 import java. util. concurrent. locks. reentrantLock; 4 5 public class NewProducerConsumerCondition 6 {7 8 public static void main (String [] args) 9 {10 Resource r = new Resource (); 11 12 // 2, create a thread task. 13 Producer pro = new Producer (r); 14 Consumer con = new Consumer (r); 15 16 // 3, create a thread. 17 Thread t1 = new Thread (pro); 18 Thread t2 = new Thread (pro); 19 Thread t3 = new Thread (con); 20 Thread t4 = new Thread (con ); 21 22 t1.start (); 23 t2.start (); 24 t3.start (); 25 t4.start (); 26} 27 28} 29 30 class Resource {31 private String name; 32 private int count = 1; 33 private final Lock lock = new ReentrantLock (); 34 private Condition produce = lock. newCondition (); 35 private Condition c Onsume = lock. newCondition (); 36 private boolean flag = false; 37 public void set (String name) 38 {39 lock. lock (); 40 try {41 while (flag) {42 try {produce. await ();} catch (Exception e) {}// Condiction object waiting 43} 44 this. name = name + count; 45 count + = 1; 46 System. out. println (Thread. currentThread (). getName () + "----- producer -------" + this. name); 47 flag = true; 48 consume. signalAll (); 49} finally {50 lock. unl Ock (); 51} 52} 53 54 public void out () 55 {56 lock. lock (); 57 try {58 while (! Flag) {59 try {consume. await ();} catch (Exception e) {} 60} 61 System. out. println (Thread. currentThread (). getName () + "----- consumer -------" + this. name); 62 flag = false; 63 produce. signalAll (); 64} finally {65 lock. unlock (); 66} 67} 68} 69 70 class Producer implements Runnable 71 {72 private Resource r; 73 // the Producer must have resources upon initialization, the resource must be passed to the constructor. 74 Producer (Resource r) 75 {76 this. r = r; 77} 78 public void run () 79 {80 while (true) 81 {82 r. set ("White Rabbit milk sugar"); 83} 84} 85} 86 87 class Consumer implements Runnable 88 {89 private Resource r; 90 // The Consumer must have resources upon initialization, the resource must be passed to the constructor. 91 Consumer (Resource r) 92 {93 this. r = r; 94} 95 public void run () 96 {97 while (true) 98 {99 r. out (); 100} 101} 102 103}View Code
Class BoundedBuffer {final Lock lock = new ReentrantLock (); // lock final Condition notFull = Lock. newCondition (); // produce final Condition notEmpty = lock. newCondition (); // the container that consumes final Object [] items = new Object [100]; // the container that stores the product. Int putptr/* the badge used by the producer */, takeptr/* the badge used by the consumer */, count/* counter */;/* the method used by the producer, store the product */public void put (Object x) throws InterruptedException {lock. lock (); try {while (count = items. length) // determine whether the counter has reached the length of the array. Full. NotFull. await (); // production is waiting. Items [putptr] = x; // store items in the array according to the badge if (++ putptr = items. length) // If the stored corner is labeled as the length of the array, the corner is converted to zero. Putptr = 0; ++ count; // counter auto-increment. NotEmpty. signal (); // wake up a consumer} finally {lock. unlock () ;}} public Object take () throws InterruptedException {lock. lock (); try {while (count = 0) // If the counter is 0, There is no commodity and the consumer waits. NotEmpty. await (); Object x = items [takeptr]; // obtain the product from the array using the consumer badge. If (++ takeptr = items. length) // if the consumed badge is equal to the length of the array, the badge is set to zero. Takeptr = 0; -- count; // counter auto-subtraction. NotFull. signal (); // wake up the producer. Return x ;}finally {lock. unlock ();}}}Stores that store multiple commodities