Java implements producer consumers

Source: Internet
Author: User

Java implements producer consumers
<span style= "FONT-SIZE:18PX;" >package Com.clark.thread;public class Mantou {public int order;public Mantou (int order) {this.order = order;} @Overridepublic String toString () {return ' Mantou: ' +this.order;}} </span>

<span Style= "FONT-SIZE:18PX;" >package com.clark.thread;//Shared stack space public class Stackbasket {public mantou[] mt = new Mantou[10];int index = 0;//production steamed bun Publ IC synchronized void push (Mantou m) {try {while (index = = mt.length) {System.out.println ("Production full!!!!!!!"); This.wait ();} This.notify ();} catch (Interruptedexception e) {e.printstacktrace ();} mt[index]=m;index++;  System.out.println ("produced:" + M + "Total" + index + "a steamed bun"); }//consumption synchronized Mantou pop () {try {while (index = = 0) {System.out.println ("consume, please wait!!!!"); This.wait ();} This.notify ();} catch (Exception e) {}index--; System.out.println ("Consumption:---------" + Mt[index] + "Total" + index + "a steamed bun"); return mt[index];}} </span> 
<span style= "FONT-SIZE:18PX;" >package com.clark.thread;//producer public class Producer implements Runnable{stackbasket SB = new Stackbasket ();p ublic Producer (Stackbasket sb) {this.sb = SB;} @Overridepublic void Run () {for (int i = 0; I <8; i++) {Mantou m = new Mantou (i); Sb.push (m); try {thread.sleep (1);} CATC H (interruptedexception e) {e.printstacktrace ();}}}} </span>

<span style= "FONT-SIZE:18PX;" >package Com.clark.thread;public class Consumer implements Runnable{stackbasket SB = new Stackbasket ();p ublic Consumer (Stackbasket sb) {this.sb = SB;} @Overridepublic void Run () {for (int i = 0; I <8; i++) {Mantou m = Sb.pop (); try {thread.sleep (1);} catch (Interruptedex Ception e) {e.printstacktrace ();}}}} </span>


<span style= "FONT-SIZE:18PX;" >package Com.clark.thread;public class Test {public static void main (string[] args) {stackbasket sb = new Stackbasket () ; Producer p = new Producer (SB); Consumer C = new Consumer (SB); thread T1 = new Thread (p); Thread t2 = new Thread (c); T1.start (); T2.start ();}} </span>


Java Implementation producer consumer

Related Article

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.