Java programming consumer and producer, java programming producer

Source: Internet
Author: User

Java programming consumer and producer, java programming producer

Create a new Break class to indicate the number of foods.

Public class Break {public static final int MAX = 10; // you can cook up to ten breads at a time. <Integer> Stack = new stack <Integer> (); public static int food_count = 0; // count the number of foods. // make a bread public synchronized void make_food (int number) {stack. push (number); food_count ++;} // eat food public synchronized int eat_food () {food_count --; return stack. pop ();}}

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

Public class Kitchen {private Break food; public static int people1_eatnumber = 0; // The number of breads that the first person eats. public static int people2_eatnumber = 0; // The second person eats public static final int TOTOAL_NUM = 100; // The total number of breads made public static int icount = 1; // count the number of breads made. public Kitchen (Break food) {this. food = food;} // method for making bread public synchronized void make () {if (food. food_count <10) {// is the pot full of food. make_food (Kitchen. icount); Syst Em. out. println ("finished" + Kitchen. icount +. "); Kitchen. icount ++; policyall (); // if the food is ready, contact two people to eat the bread} else {System. out. println ("the pot is full and ten are loaded at a time, waiting for the two to eat food. "); Try {wait ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} // enter the blocking status and wait for the consumer to wake up} // public synchronized void eat () {if (food. food_count> 0) {int temp = food. eat_food (); // The number of the bread. out. println (Thread. currentThread (). getName () + "eat" + temp + "food"); if (Thread. currentThread (). getName (). equals ("1") {people1_eatnumber ++;} else {people2_eatnumber + +;} Yyall (); // wake up and continue to make the bread} else {System. out. println ("the food has been eaten, waiting for a new cooking. "); Try {wait ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}

The following describes the structure of the consumer and producer classes and passes them to a Kitchen object. The consumer calls the eating method in the Kitchen class.

public class consumer implements Runnable {    Kitchen kitchen;        public consumer(Kitchen kitchen){        this.kitchen = kitchen;    }    @Override    public void run() {        // 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();            }        }    }}

However, the producer calls the production method in the Kitchen object.

Public class product implements Runnable {private Kitchen kitchen; public product (Kitchen kitchen) {this. kitchen = kitchen ;}@ Override public void run () {// 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 is finished. ");}}

The following describes how to write the main class, including the main method, start the program, and create a test class:

Public class test {public static void main (String [] args) {Break brake = new Break (); Kitchen kitchen = new Kitchen (brake ); product p = new product (kitchen); consumer c1 = new consumer (kitchen); consumer c2 = new consumer (kitchen); Thread food = new Thread (p, "food "); thread lele1 = new Thread (c1, "1"); Thread people2 = new Thread (c2, "2"); food. start (); people1.start (); people2.start (); try {food. join (); people1.join (); people2.join ();} catch (Exception e) {e. printStackTrace ();} System. out. println ("I have eaten" + Kitchen. lele1_eatnumber); System. out. println ("2 eat" + Kitchen. people2_eatnumber );}}

The output result is as follows:

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

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.