The producer/consumer model of the Java Multithreaded 15:wait () and notify () has been implemented in this chapter Wait/notify production consumption models
Using await ()/signal () to implement producer and consumer models
, define a buffer first:
public class valueobject{ Public static String value = ";}
In a different way, the production and consumption methods are placed in a class:
Public classThreadDomain41extendsreentrantlock{PrivateCondition Condition =newcondition (); Public voidset () {Try{lock (); while(!"". Equals (Valueobject.value)) condition.await (); Valueobject.value= "123"; System.out.println (Thread.CurrentThread (). GetName ()+ "value is produced, the current value of value is" +valueobject.value); Condition.signal (); } Catch(interruptedexception e) {e.printstacktrace (); } finally{unlock (); } } Public voidget () {Try{lock (); while("". Equals (Valueobject.value)) condition.await (); Valueobject.value= ""; System.out.println (Thread.CurrentThread (). GetName ()+ "value is consumed, the current value of value is" +valueobject.value); Condition.signal (); } Catch(interruptedexception e) {e.printstacktrace (); } finally{unlock (); } }}
Similarly, open two threads, one thread calls the set () method for production, and the other thread calls the Get () method to consume:
Public Static voidMain (string[] args) {FinalThreadDomain41 td =NewThreadDomain41 (); Runnable producerrunnable=NewRunnable () { Public voidrun () { for(inti = 0; i < Integer.max_value; i++) Td.set (); } }; Runnable customerrunnable=NewRunnable () { Public voidrun () { for(inti = 0; i < Integer.max_value; i++) Td.get (); } }; Thread Producerthread=NewThread (producerrunnable); Producerthread.setname ("Producer"); Thread Consumerthread=NewThread (customerrunnable); Consumerthread.setname ("Consumer"); Producerthread.start (); Consumerthread.start ();}
Look at the results of the operation:
... Producer produced value, the current value is 123Consumer consumption of value, value of the current value is producer produced value, value of the current value is 123Consumer consumption of value, The current value of value is producer produced value, the current value of value is 123Consumer consumes value, the current value of value is ...
Same as the Wait ()/notify () mechanism, as well as with the producer/consumer model
Beware of suspended animation
The producer/consumer model ultimately achieves the goal of balancing the processing capacity of producers and consumers , and does not require only one producer and one consumer in the process of achieving this goal. Multiple producers can correspond to multiple consumers, one producer can correspond to a consumer, and multiple producers can correspond to one consumer.
Suspended animation occurs in three of the above scenarios. Theoretical analysis can explain the problem, so you do not write code. Code to write is also very simple, the above two examples casually modify one, open a producer thread/multiple consumer threads, open multiple producer threads/consumer threads, open multiple producer threads/multiple consumer threads can be. Suspended animation refers to all the threads have entered the waiting state, then the program will no longer perform any business functions, the entire project is stuck.
For example, there are producer A and producer B, the buffer is empty and the consumer is waiting. Producer B is in waiting, producer A is informed by the consumer production, producer a produced products should inform consumers, the results inform the producer B, producer B was awakened, found that the buffer is full, so continue to waiting. At this point, two producers of threads in waiting, consumers in waiting, the system suspended animation.
The above analysis can be seen that the reason for the occurrence of suspended animation is because notify is similar, so non-single-producer/single-consumer scenarios, can take two methods to solve the problem:
1. Synchronized wakes all threads with Notifyall (), Reentrantlock wakes all threads with Signalall ()
2, with Reentrantlock definition two condition, a producer of condition, a consumer condition, wake up call the corresponding condition signal () method can be
here the contrast and wait/notify, await ()/signal () can take advantage of multiple Condition for consumption/production implementation, without notifying all threads, where it appears more efficient and convenient
to achieve the production consumption model, Java provides a more convenient implementation of the queue mechanism, referenceJava multi-threaded blocking queue and blocking method and non-blocking method
Java Multi-Threading 22: Producer/Consumer Mode Advanced utilization await ()/signal () implementation