Java Multi-Threading and concurrency--a case of producer and consumer applications

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.