Java thread simulation producer and consumer

Source: Internet
Author: User

Public class producerconsumer <br/>{< br/> Public static void main (string [] ARGs) <br/>{< br/> syncstack sync = new syncstack (); <br/> producer pro = new producer (Sync); <br/> consumer con = new consumer (Sync); </P> <p> for (INT I = 0; I <1; I ++) <br/>{< br/> New thread (Pro, String. valueof (I )). start (); <br/> New thread (con, String. valueof (I )). start (); <br/>}</P> <p>/** <br/> * docker stack <br/> **/< br/> class syncstack <br/>{< br/> private woto [] Wos = new woto [600]; <br/> private int index; </P> <p> Public synchronized void push (woto) <br/>{< br/> while (WOS. length = index) <br/>{< br/> try <br/>{< br/> This. wait (); <br/>}< br/> catch (interruptedexception e) <br/>{< br/> E. printstacktrace (); <br/>}</P> <p> This. using Y (); </P> <p> WOS [Index] = woto; <br/> ++ index; <br/> system. out. println (thread. currentthread (). getname () + "_____________ produced steamed bread->" + woto); <br/>}</P> <p> Public synchronized woto POP () <br/>{< br/> while (0 = index) <br/>{< br/> try <br/>{< br/> This. wait (); <br/>}< br/> catch (interruptedexception e) <br/>{< br/> E. printstacktrace (); <br/>}</P> <p> This. using Y (); <br/> -- index; <br/> system. out. println (thread. currentthread (). getname () + "% consumed steamed buns->" + WOS [Index]); <br/> return WOS [Index]; </P> <p >}< br/>}</P> <p>/** <br/> * Header class <br/> **/<br/> class woto <br/>{< br/> private string name; </P> <p> Public woto (string name) <br/>{< br/> This. name = Name; <br/>}</P> <p> Public String tostring () <br/>{< br/> return "[nest header:" + this. name + "]"; <br/>}</P> <p>/** <br/> * producer <br/> **/<br/> class producer implements runnable <br/>{< br/> private syncstack wotos; </P> <p> Public producer (syncstack wotos) <br/>{< br/> This. wotos = wotos; <br/>}</P> <p> Public void run () <br/>{< br/> for (INT I = 0; I <2000; I ++) <br/>{< br/> woto = new woto (string. valueof (I); <br/> This. wotos. push (woto); </P> <p> // try <br/> // {<br/> // thread. sleep (INT) (math. random () * 2); <br/> //} <br/> // catch (interruptedexception E) <br/> // {<br/> // E. printstacktrace (); <br/>/}< br/>}</P> <p>/** <br/> * Consumer <br /> **/<br/> class Consumer implements runnable <br/> {<br/> private syncstack wotos; </P> <p> public consumer (syncstack wotos) <br/>{< br/> This. wotos = wotos; <br/>}</P> <p> Public void run () <br/>{< br/> for (INT I = 0; I <2000; I ++) <br/>{< br/> This. wotos. pop (); </P> <p> try <br/>{< br/> thread. sleep (INT) (math. random () * 2); <br/>}< br/> catch (interruptedexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/>

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.