Multi-threaded development has one of the most classic operating cases, that is, producers-consumers, producers continue to produce products, consumers continue to take away products.
Packagecom.vince;/*** Producer and Consumer cases *@authorAdministrator **/ Public classThreadDemo4 { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsFood food=NewFood (); Producter P=Newproducter (food); Customer C=NewCustomer (food); Thread T1=NewThread (P); Thread T2=NewThread (c); T1.start (); T2.start (); }}//producersclassProducterImplementsrunnable{PrivateFood and food ; Publicproducter (food food) { This. food=Food ; } @Override Public voidrun () {//TODO Auto-generated method stubs for(inti=0;i<50;i++){ if(i%2==0){ /*food.setname ("Tremella lotus Soup"); try {thread.sleep (500); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); } food.setefficasy ("Beauty Facial");*/Food.set ("White fungus and lotus seed soup", "Beauty Facial"); }Else{ /*food.setname ("Boiled sliced meat"); try {thread.sleep (500); } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); } food.setefficasy ("fattening");*/Food.set ("Boiled sliced Meat", "fattening"); } } } }//ConsumerclassCustomerImplementsrunnable{PrivateFood and food ; PublicCustomer (food) { This. food=Food ; } @Override Public voidrun () {//TODO Auto-generated method stubs for(inti=0;i<50;i++) {food.get (); } } }//object of consumption (data)classfood{PrivateString name;//Dish name PrivateString Efficasy;//Efficacy Private Booleanflag=true;//True indicates that production cannot be consumed and false means that it can consume PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getefficasy () {returnEfficasy; } Public voidSetefficasy (String efficasy) { This. Efficasy =Efficasy; } PublicFood (string name, String efficasy) {Super(); This. Name =name; This. Efficasy =Efficasy; } PublicFood () {Super(); //TODO Auto-generated constructor stub } //Production Products Public synchronized voidSet (String name,string efficasy) {//indicates inability to produce if(!flag) { Try { This. Wait ();//The current thread enters the wait state, yields the CPU, and releases the lock on the monitor. }Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } } This. SetName (name); Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } This. Setefficasy (Efficasy); Flag=false;//indicates no further production This. Notify ();//Wake up one of the other threads on the monitor } Public synchronized voidget () {if(flag) {Try { This. Wait (); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } } Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } System.out.println ( This. GetName () + "+" + This. Getefficasy ()); Flag=true;//It means no more spending . This. Notify ();//Wake up one of the other threads on the monitor }}
Java Multi-Threading and concurrency--a case of producer and consumer applications