Multi-threading, producer consumer model (production of steamed bread, consumption of steamed bread)

Source: Internet
Author: User

Create a container first

/*** Container * shared resources *@authorAdministrator **/ Public classSynstack {intindex = 0; //Containersteambread[] STB =NewSteambread[6]; /*** Placing products in containers*/     Public synchronized voidpush (Steambread St) { while(Index = =stb.length) {            Try {                 This. Wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} notify ();//waking a waiting threadStb[index] =St;  This. index++; }    /*** Take products from the container*/     Public synchronizedsteambread Pop () { while(Index = = 0){            Try {                 This. Wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();        }} notify ();  This. index--; returnStb[index]; }}

Production of steamed bread

 Packagecom.newer.cn; Public classProducerImplementsrunnable{Synstack SS=NULL;  PublicProducer (Synstack ss) {//TODO auto-generated Constructor stub         This. SS =SS; } @Override Public voidrun () {//started production of steamed bread         for(inti = 1;i <= 20;i++) {steambread STB=NewSteambread (i); System.out.print ("Produced by::::::");            Ss.push (STB); System.out.println ("Produced" +STB); Try{Thread.Sleep (5000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }            }}

Consumption steamed Bread

 Packagecom.newer.cn; Public classConsumeImplementsrunnable{Synstack SS=NULL;  Publicconsume (Synstack ss) { This. SS =SS; } @Override Public voidrun () {//started to consume steamed bread         for(inti = 1;i <= 20;i++) {System.out.print ("Consumption::-::::"); Steambread STB=Ss.pop (); System.out.println ("Consumption" +STB); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }            }}

Test

 Packagecom.newer.cn; Public classTest2 { Public Static voidMain (string[] args) {synstack ss=NewSynstack (); //Producer ThreadsProducer p =NewProducer (ss); Thread TP=NewThread (P); //Consumer ThreadsConsume C =Newconsume (ss); Thread TC=NewThread (c);        Tp.start ();    Tc.start (); }}

Multi-threading, producer consumer model (production of steamed bread, consumption of steamed bread)

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.