Before I say the producer-consumer model, I think it is necessary to understand the obj.wait () and the Obj.notify () method. The wait () method means that when a thread holding an object lock calls this method, the object lock is freed and the thread is dormant. The Notify () method is to hold the same object lock to wake the dormant thread, making it eligible to preempt the CPU. Can understand the synchronous method, the object lock of the synchronous method is who calls this method, this object is the object lock.
According to Li Xinghua Teacher's video explanation, to build a producer class, a consumer class, there is an info class, affixed with the code:
1. Producer Class
PackageCom.company;/*** Created by Administrator on 2016/8/30.*/ Public classProductorImplementsRunnable {PrivateInfo Info; PublicProductor (Info info) { This. info =info; } @Override Public voidrun () { for(inti=0;i<50;i++) { if(i%2==0) {//info.settitle ("Tao");//info.setcontent ("a Handsome Man");//try {//Thread.Sleep (+);//} catch (Interruptedexception e) {//e.printstacktrace ();// }Info.set ("Tao", "a handsome Man"); }Else {//info.settitle ("lovely Animal");//info.setcontent ("Grass mud Horse");Info.set ("Cute Animal", "Grass Mud Horse"));//try {//Thread.Sleep (+);//} catch (Interruptedexception e) {//e.printstacktrace ();// } } } }}
2. Consumer Class
PackageCom.company;/*** Created by Administrator on 2016/8/30.*/ Public classConsumerImplementsRunnable {PrivateInfo Info; PublicConsumer (Info info) { This. info =info; } @Override Public voidrun () { for(inti=0;i<50;i++) { Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } info.get (); } }}
3.Info Class (emphasis)
PackageCom.company;/*** Created by Administrator on 2016/8/30.*/ Public classInfo {PrivateString Title; PrivateString content; Private Booleanflag=true;//True indicates that production can not be taken away from consumption, false opposite Public synchronized voidSet (String titile,string content) {if(!flag) { Try{wait (); } Catch(interruptedexception e) {e.printstacktrace (); } } This. title=Titile; This. content=content; Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } Flag=false; Notify (); } Public synchronized voidget () {if(flag) {Try{wait (); } Catch(interruptedexception e) {e.printstacktrace (); } } Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ( This. Title+ "------->" + This. Content); Flag=true; Notify (); }//Public String GetTitle () {//return Title;// }////Public void Settitle (String title) {//title = title;// }////Public String getcontent () {//return content;// }////Public void SetContent (String content) {//this.content = content;// }}
4. Test class
package Com.company; public class Main { static void main (string[] args) {Info Info =new info (); Productor p =new Productor (info); Consumer c =new Consumer (info); new Thread (p). Start (); new Thread (c). Start (); }}
Summary analysis: The producer and consumer two threads simultaneously preemption CPU, if the consumer first grab, at this time call get () method namely consume take away, but at this time flag is true, therefore will enter wait () method, so at this time can only be producer preemption to CPU, according to producer Call Set () method, Post-production execution
Flag=false; Notify ();
At this time the producers and consumers in the two threads and stand on the same starting line, continue the CPU scramble. If the unfortunate consumer has not robbed the producer, the producer continues to execute set (); flag is now false, according to the code
if (! flag) { try { wait (); Catch (interruptedexception e) { e.printstacktrace (); } }
So the producer can only obediently wait (); The CPU returns to the consumer here, the cycle, the pattern is out, perfect!
Understanding of producer consumer patterns in Java