Consumers and producers of Java programming

Source: Internet
Author: User

Creates a new break class that represents the quantity of food.

 Public classBreak { Public Static Final intMAX = 10;//Cook up to 10 loaves of bread at oncestack<integer> stack =NewStack<integer>();  Public Static intFood_count = 0;//count the number of food//Make Bread     Public synchronized voidMake_food (intNumber )        {Stack.push (number); Food_count++; }        //Eat food     Public synchronized intEat_food () {Food_count--; returnStack.pop (); }}

In creating a kitchen class, in order to make food or consume food, if the food is not there, the consumer enters wait (), the producer starts to make the food, and then wakes the waiting consumer.

 Public classKitchen {PrivateBreak food ;  Public Static intPeople1_eatnumber = 0;//the number of bread the first person eats     Public Static intPeople2_eatnumber = 0;//the number of loaves eaten by the second person     Public Static Final intTotoal_num = 100;//total amount of bread made     Public Static inticount = 1;//Count The amount of bread that has been made         PublicKitchen (break food) { This. Food =Food ; }        //the way to make bread     Public synchronized voidMake () {if(FOOD.FOOD_COUNT&LT;10) {//the pot is not fullFood.make_food (Kitchen.icount); System.out.println ("Finished the first" +kitchen.icount+ "food. "); Kitchen.icount++; Notifyall ();//The food is there, find two people to eat bread}Else{System.out.println ("The pot is full, one-time pack of 10, waiting for two people to eat food." "); Try{wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }//into a blocking state, waiting for the consumer to wake up        }    }        //the way to eat bread     Public synchronized voideat () {if(food.food_count>0){            inttemp = Food.eat_food ();//the number of the bread eatenSystem.out.println (Thread.CurrentThread (). GetName () + "ate the first" +temp+ "food"); if(Thread.CurrentThread (). GetName (). Equals ("Number One")) ) {People1_eatnumber++; }Else{People2_eatnumber++;  } notifyall (); //wake up and continue making bread}Else{System.out.println ("The food is finished and waiting to be re-cooked. "); Try{wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }}

The following goes into the structure of the consumer and producer classes, passed to a kitchen object, where the consumer invokes the kitchen class in the eating method.

 Public classConsumerImplementsRunnable {Kitchen Kitchen;  Publicconsumer (Kitchen Kitchen) { This. Kitchen =Kitchen; } @Override Public voidrun () {//TODO auto-generated Method Stub         while(true){            if(kitchen.icount>100&&break.food_count<=0){                 Break; }            Try{kitchen.eat (); Thread.Sleep (100); }Catch(Exception e) {e.printstacktrace (); }        }    }}

The producer, however, invokes the authoring method in the kitchen object.

 Public classProductImplementsRunnable {PrivateKitchen Kitchen;  Publicproduct (Kitchen Kitchen) { This. Kitchen =Kitchen; } @Override Public voidrun () {//TODO auto-generated Method Stub         while(true){            //End this thread            if(Kitchen.icount > 100){                 Break; }            Try{kitchen.make (); Thread.Sleep (100); }Catch(Exception e) {e.printstacktrace (); }} System.out.println ("The kitchen food material has been finished. "); }}

The following begins the main class, including the main method, starting the program, and creating a test class that implements:

 Public classTest { Public Static voidMain (string[] args) {Break brake=NewBreak (); Kitchen Kitchen=NewKitchen (brake); Product P=Newproduct (kitchen); Consumer C1=Newconsumer (kitchen); Consumer C2=Newconsumer (kitchen); Thread Food=NewThread (p, "food"); Thread People1=NewThread (C1, "number One")); Thread People2=NewThread (C2, "number second");        Food.start ();        People1.start ();                People2.start (); Try{food.join ();            People1.join ();        People2.join (); }Catch(Exception e) {e.printstacktrace (); } System.out.println ("One eat" +kitchen.people1_eatnumber); System.out.println ("Number second Eats" +kitchen.people2_eatnumber); }}

The output results are as follows:

Finished the 1th food.
Number second eats a 1th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 2nd food.
The first one ate the 2nd food,
The food is finished and waiting to be re-cooked.
Finished the 3rd food.
The first one ate the 3rd food,
The food is finished and waiting to be re-cooked.
Finished the 4th food.
The first one ate the 4th food,
Finished the 5th food.
Number second eats a 5th food.
The food is finished and waiting to be re-cooked.
Finished the 6th food.
Number second eats a 6th food.
The food is finished and waiting to be re-cooked.
Finished the 7th food.
Number second eats a 7th food.
Finished the 8th food.
The first one ate the 8th food,
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 9th food.
The first one ate the 9th food,
The food is finished and waiting to be re-cooked.
Finished the 10th food.
The first one ate the 10th food,
Finished the 11th food.
Number second eats a 11th food.
The food is finished and waiting to be re-cooked.
Finished the 12th food.
Number second eats a 12th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 13th food.
The first one ate the 13th food,
Finished the 14th food.
Number second eats a 14th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 15th food.
The first one ate the 15th food,
Finished the 16th food.
Number second eats a 16th food.
The food is finished and waiting to be re-cooked.
Finished the 17th food.
Number second eats a 17th food.
The food is finished and waiting to be re-cooked.
Finished the 18th food.
Number second eats a 18th food.
The food is finished and waiting to be re-cooked.
Finished the 19th food.
Number second eats a 19th food.
The food is finished and waiting to be re-cooked.
Finished the 20th food.
Number second eats a 20th food.
The food is finished and waiting to be re-cooked.
Finished the 21st food.
Number second eats a 21st food.
The food is finished and waiting to be re-cooked.
Finished the 22nd food.
Number second eats a 22nd food.
The food is finished and waiting to be re-cooked.
Finished the 23rd food.
Number second eats a 23rd food.
The food is finished and waiting to be re-cooked.
Finished the 24th food.
Number second eats a 24th food.
The food is finished and waiting to be re-cooked.
Finished the 25th food.
Number second eats a 25th food.
The food is finished and waiting to be re-cooked.
Finished the 26th food.
Number second eats a 26th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 27th food.
The first one ate the 27th food,
The food is finished and waiting to be re-cooked.
Finished the 28th food.
The first one ate the 28th food,
The food is finished and waiting to be re-cooked.
Finished the 29th food.
The first one ate the 29th food,
Finished the 30th food.
Number second eats a 30th food.
The food is finished and waiting to be re-cooked.
Finished the 31st food.
Number second eats a 31st food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 32nd food.
The first one ate the 32nd food,
The food is finished and waiting to be re-cooked.
Finished the 33rd food.
The first one ate the 33rd food,
The food is finished and waiting to be re-cooked.
Finished the 34th food.
The first one ate the 34th food,
Finished the 35th food.
Number second eats a 35th food.
The food is finished and waiting to be re-cooked.
Finished the 36th food.
Number second eats a 36th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 37th food.
The first one ate the 37th food,
The food is finished and waiting to be re-cooked.
Finished the 38th food.
The first one ate the 38th food,
The food is finished and waiting to be re-cooked.
Finished the 39th food.
The first one ate the 39th food,
Finished the 40th food.
Number second eats a 40th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 41st food.
The first one ate the 41st food,
Finished the 42nd food.
Number second eats a 42nd food.
Finished the 43rd food.
Number second eats a 43rd food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 44th food.
The first one ate the 44th food,
The food is finished and waiting to be re-cooked.
Finished the 45th food.
The first one ate the 45th food,
Finished the 46th food.
Number second eats a 46th food.
The food is finished and waiting to be re-cooked.
Finished the 47th food.
Number second eats a 47th food.
The food is finished and waiting to be re-cooked.
Finished the 48th food.
Number second eats a 48th food.
The food is finished and waiting to be re-cooked.
Finished the 49th food.
Number second eats a 49th food.
The food is finished and waiting to be re-cooked.
Finished the 50th food.
Number second eats a 50th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 51st food.
The first one ate the 51st food,
The food is finished and waiting to be re-cooked.
Finished the 52nd food.
The first one ate the 52nd food,
Finished the 53rd food.
Number second eats a 53rd food.
Finished the 54th food.
The first one ate the 54th food,
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 55th food.
Finished the 56th food.
Number second eats a 56th food.
The first one ate the 55th food,
Finished the 57th food.
The first one ate the 57th food,
The food is finished and waiting to be re-cooked.
Finished the 58th food.
The first one ate the 58th food,
Finished the 59th food.
Number second eats a 59th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 60th food.
The first one ate the 60th food,
Finished the 61st food.
Number second eats a 61st food.
The food is finished and waiting to be re-cooked.
Finished the 62nd food.
Number second eats a 62nd food.
The food is finished and waiting to be re-cooked.
Finished the 63rd food.
Number second eats a 63rd food.
The food is finished and waiting to be re-cooked.
Finished the 64th food.
Number second eats a 64th food.
The food is finished and waiting to be re-cooked.
Finished the 65th food.
Number second eats a 65th food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 66th food.
Finished the 67th food.
Number second eats a 67th food.
The first one ate the 66th food,
Finished the 68th food.
The first one ate the 68th food,
The food is finished and waiting to be re-cooked.
Finished the 69th food.
The first one ate the 69th food,
The food is finished and waiting to be re-cooked.
Finished the 70th food.
The first one ate the 70th food,
Finished the 71st food.
Number second eats a 71st food.
The food is finished and waiting to be re-cooked.
Finished the 72nd food.
Number second eats a 72nd food.
The food is finished and waiting to be re-cooked.
Finished the 73rd food.
Number second eats a 73rd food.
The food is finished and waiting to be re-cooked.
The food is finished and waiting to be re-cooked.
Finished the 74th food.
The first one ate the 74th food,
The food is finished and waiting to be re-cooked.
Finished the 75th food.
The first one ate the 75th food,
Finished the 76th food.
Number second eats a 76th food.
The food is finished and waiting to be re-cooked.
Finished the 77th food.
Number second eats a 77th food.
The food is finished and waiting to be re-cooked.
Finished the 78th food.
Number second eats a 78th food.
The food is finished and waiting to be re-cooked.
Finished the 79th food.
Number second eats a 79th food.
The food is finished and waiting to be re-cooked.
Finished the 80th food.
Number second eats a 80th food.
The food is finished and waiting to be re-cooked.
Finished the 81st food.
Number second eats a 81st food.
The food is finished and waiting to be re-cooked.
Finished the 82nd food.
Number second eats a 82nd food.
The food is finished and waiting to be re-cooked.
Finished the 83rd food.
Number second eats a 83rd food.
The food is finished and waiting to be re-cooked.
Finished the 84th food.
Number second eats a 84th food.
The food is finished and waiting to be re-cooked.
Finished the 85th food.
Number second eats a 85th food.
The food is finished and waiting to be re-cooked.
Finished the 86th food.
Number second eats a 86th food.
The food is finished and waiting to be re-cooked.
Finished the 87th food.
Number second eats a 87th food.
The food is finished and waiting to be re-cooked.
Finished the 88th food.
Number second eats a 88th food.
Finished the 89th food.
Number second eats a 89th food.
The food is finished and waiting to be re-cooked.
Finished the 90th food.
Number second eats a 90th food.
The food is finished and waiting to be re-cooked.
Finished the 91st food.
Number second eats a 91st food.
The food is finished and waiting to be re-cooked.
Finished the 92nd food.
Number second eats a 92nd food.
The food is finished and waiting to be re-cooked.
Finished the 93rd food.
Number second eats a 93rd food.
The food is finished and waiting to be re-cooked.
Finished the 94th food.
Number second eats a 94th food.
The food is finished and waiting to be re-cooked.
Finished the 95th food.
Number second eats a 95th food.
The food is finished and waiting to be re-cooked.
Finished the 96th food.
Number second eats a 96th food.
The food is finished and waiting to be re-cooked.
Finished the 97th food.
Number second eats a 97th food.
The food is finished and waiting to be re-cooked.
Finished the 98th food.
Number second eats a 98th food.
The food is finished and waiting to be re-cooked.
Finished the 99th food.
Number second eats a 99th food.
The food is finished and waiting to be re-cooked.
Finished the 100th food.
Number second eats a 100th food.
The kitchen food material has been finished.
I ate 33.
Number second eats 67.

Consumers and producers of Java programming

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.