Java Thread Producerandconsumer

Source: Internet
Author: User
Tags ack

Package j2se.thread.demo;/** * &LT;P&GT;PROJECT:J2SE Basics </p> * <p>tile: Multithreaded simulation for producers and consumers </p> * &LT;P&GT  ;D escription: * * * </p> * * @date 2014-06-10 * @author Liwenkai * @version 1.0 * */public class Producerandconsumer {public static void main (string[] args) {stack s = new Stack (6); Producer p = new Producer (s); Consumer C = new Consumer (s); Thread TP = new Thread (p, "product"); Tp.start (); try {///main thread main thread first sleep Millis, let the producer create product Thread.Sleep first;} catch (Int Erruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Thread TC = new Thread (c, "product"); Tc.start ();}} Defines the product class, item {public string ID, item (String _id) {id = _id;}};/    /Simulate a stack (stack) container class Stack {item[] arr; int index;   Stack (int _size) {arr = new item[_size];    index = 0;}  Public synchronized void push (Item _item) {while (index = = arr.length) {try {//index = = Arr.length line Wait,   Block off this.wait (); } catch (InterruptedexcEption e) {e.printstacktrace ();}     } this.notify ();   Arr[index] = _item;   index++; The index read here is the most accurate System.out.println ("Push Consumer index =" + Index + ", Current time =" +system.c Urrenttimemillis ());}      Public synchronized Item POPs () {while (index = = 0) {try {///When index = = 0, consumer thread wait, block off;   This.wait ();   } catch (Interruptedexception e) {e.printstacktrace ();      }} this.notify ();   index--; The index read here is the most accurate System.out.println ("Pop Consumer index =" + Index + ", Current time =" +system.cu   Rrenttimemillis ()); return Arr[index];} public synchronized int GetIndex () {return index;}};/ /producer Class Producer implements runnable{Stack s; Producer (Stack _s) {s = _s;}  public void Run () {for (int i = 0; i <; i++) {Item item = new Item (Thread.CurrentThread (). GetName () + i) S.push (item),////Stack.index = S.getindex (), which may not be the result of producer thread churn,//The value can be changed because the consumer thread is also executing; SysTem.out.println ("Production of products \" "+ item.id +" \ "This is" +thread.currentthread (). GetName () + "production of" + i + "products," + "Stack.index =" + s.getindex () + ", curent time =" +system.currenttimemillis ()); Try{thread.sleep ((int) (Math.random () *1000));}    catch (Interruptedexception e) {e.printstacktrace ();} }}}//Consumer class Consumer implements runnable{Stack s; Consumer (Stack _s) {s = _s;} public void Run () {for (int i = 0; i <; i++) {Item item = S.POP ();//The Stack.index received here = S.getindex (), may not be consumed The value after the thread is changed,//because the producer thread is also executing, the value can be changed; System.out.println ("Consumption of products \" "+ item.id +" \ "This is" +thread.currentthread (). GetName () + "Consumption of the first" + i + "products" + "," + "stack.in        Dex = "+ s.getindex () +", Current time = "+system.currenttimemillis ()"); Try{thread.sleep ((int) (Math.random () *1000));} catch (Interruptedexception e) {e.printstacktrace ();}} }}/** * Push Consumer index = 1, current time = 1402393225000 produced the product "Product 0" This is the No. 0 product of the production, Stack.index = 1, curent time = 1 402393225000POP Consumer INDex = 0, current time = 1402393225109 consumes the product "product 0" This is the No. 0 product of the product consumption, Stack.index = 0, current time = 1402393225109push consume R index = 1, current time = 1402393225718pop Consumer index = 0, current time = 1402393225718 consumption of product "Product 1" This is the 1th product of product consumption,  Stack.index = 0, current time = 1402393225718 produced the product "Product 1" This is the product of the production of the 1th product, Stack.index = 0, curent time = 1402393225718push Consumer index = 1, current time = 1402393226718pop Consumer index = 0, current duration = 1402393226718 consumption of product "Product 2" This is the product consumption 2nd product, Stack.index = 0, current time = 1402393226718 produced the product "Product 2" This is the production of the 2nd product, Stack.index = 0, curent time = 140239322 6718push Consumer index = 1, current time = 1402393227218 produced the product "Product 3" This is the product of the production of the 3rd product, Stack.index = 1, curent time = 14023 93227218pop Consumer index = 0, current time = 1402393227453 consumes the product "Product 3" This is the product of the 3rd product consumption, Stack.index = 0, Current time = 1 402393227453push Consumer index = 1, current time = 1402393228109pop Consumer index = 0, and current = 1402393228109 produced Product "Product 4" This is the product of the 4th product production,Stack.index = 0, curent time = 1402393228109 consumes the product "Product 4" This is the product of the 4th product consumption, Stack.index = 0, Current time = 1402393228109push Consumer index = 1, current time = 1402393228312 produced the product "Product 5" This is the product of the production of the 5th product, Stack.index = 1, curent time = 1402393228312 Pop Consumer index = 0, current time = 1402393228390 consumes the product "product 5" This is the product of the 5th product consumption, Stack.index = 0, Current time = 140239322 8390push Consumer index = 1, current time = 1402393228593 produced the product "Product 6" This is the product of the production of the 6th product, Stack.index = 1, curent time = 14023 93228593pop Consumer index = 0, current time = 1402393228593 consumes the product "product 6" This is the product of the 6th product consumption, Stack.index = 0, Current time = 1 402393228593push Consumer index = 1, current time = 1402393228890pop Consumer index = 0, current duration = 1402393228890 consumed Product 7 This is the product consumption of the 7th product, Stack.index = 0, current time = 1402393228890 produced the product "Product 7" This is the production of the 7th product, Stack.index = 0, curent Time = 1402393228890push consumer the index = 1, and current duration = 1402393229312 produces the product "Product 8" which is the 8th product of the production, Stack.index = 1, cur ent time = 1402393229312Pop Consumer index = 0, current time = 1402393229890 consumes the product "Product 8" This is the product of the 8th product consumption, Stack.index = 0, Current time = 140239322 9890push Consumer index = 1, current time = 1402393230000 produced the product "Product 9" This is the product of the production of the 9th product, Stack.index = 1, curent time = 14023  93230000push Consumer index = 2, current time = 1402393230359 produced the product "Product 10" This is the product of the production of the 10th product, Stack.index = 2, curent time = 1402393230359pop Consumer index = 1, current time = 1402393230765 consumes the product "product 10" This is the 9th product of product consumption, Stack.index = 1, current t IME = 1402393230765push Consumer index = 2, current time = 1402393230781 produced the product "Product 11" This is the 11th product of the production, Stack.index = 2, cu  Rent time = 1402393230781pop Consumer index = 1, current duration = 1402393230890 consumes the product "product 11" This is the 10th product of product consumption, Stack.index = 1 , Current time = 1402393230890push Consumer index = 2, and current duration = 1402393230921 produced the product "Product 12" This is the 12th product of the production, stack.in Dex = 2, curent time = 1402393230921push Consumer index = 3, current = 1402393231390 produced product "Product 13" This is the 13th product of the production, St Ack.index = 3, curent tIME = 1402393231390pop Consumer index = 2, current time = 1402393231625 consumes the product "product 13" This is the product of the 11th product consumption, Stack.index = 2, cur Rent time = 1402393231625push Consumer index = 3, current duration = 1402393231656 produced product "Product 14" This is the 14th product of the production, Stack.index = 3, curent time = 1402393231656push Consumer index = 4, current duration = 1402393231984 produced the product "Product 15" This is the 15th product of the production, stack.in Dex = 4, curent time = 1402393231984push Consumer index = 5, current = 1402393232125 produced product "Product 16" This is the 16th product of the production, St Ack.index = 5, curent time = 1402393232125push Consumer index = 6, current = 1402393232250 produced product "Product 17" This is the 17th production of the product Product, Stack.index = 6, curent time = 1402393232250pop Consumer index = 5, current duration = 1402393232531 consumption of product "products 17" This is the product consumption  The 12th product, Stack.index = 5, current time = 1402393232531push Consumer index = 6, 1402393233234 produced product "Product 18" This is the product of the 18th product, Stack.index = 6, curent time = 1402393233234pop Consumer index = 5, the current duration = 1402393233421 consumption of products "products 18 "This is the 13th product of product consumption, STACk.index = 5, current time = 1402393233421pop Consumer index = 4, current time = 1402393233921 consumption of product "product 16" This is the 14th product of product consumption , Stack.index = 4, current time = 1402393233921push Consumer index = 5, and current duration = 1402393234218 produced product "Product 19" This is the production of products Of the 19th product, Stack.index = 5, curent time = 1402393234218pop Consumer index = 4, current duration = 1402393234828 consumes the product "product 19" this is the product consumption of the 15th product, Stack.index = 4, current time = 1402393234828push Consumer index = 5, the current 1402393235187 produced products "production Product 20 "This is the 20th product of production, Stack.index = 5, curent time = 1402393235187push Consumer index = 6, current time = 1402393235609 Raw Product "Product 21" This is the product of the production of the 21st product, Stack.index = 6, curent time = 1402393235609pop Consumer index = 5, Current time = 140239323 5796 consumed the product "product 21" This is the product consumption of the 16th product, Stack.index = 5, current time = 1402393235796push Consumer index = 6, Current time = 14  02393235875 produced the product "product 22" This is the production of the 22nd product, Stack.index = 6, curent time = 1402393235875pop Consumer index = 5, current time = 1402393236656 consumption of products "products22 "This is the 17th product of the product consumption, Stack.index = 5, current time = 1402393236656push Consumer index = 6, current time = 1402393236859 Raw Product "Product 23" This is the product of the production of the 23rd product, Stack.index = 6, curent time = 1402393236859pop Consumer index = 5, Current time = 140239323 7250 consumed the product "product 23" This is the product consumption of the 18th product, Stack.index = 5, current time = 1402393237250push Consumer index = 6, Current time = 14  02393237812 produced the product "product 24" This is the production of the 24th product, Stack.index = 6, curent time = 1402393237812pop Consumer index = 5, current time = 1402393238187 consumption of the product "product 24" This is the product consumption of the 19th product, Stack.index = 5, current time = 1402393238187push Consumer index = 6, Curre NT time = 1402393238218 produced the product "product 25" This is the product of the production of the 25th product, Stack.index = 6, curent time = 1402393238218pop Consumer index = 5,  Current time = 1402393238937push Consumer the index = 6, and current duration = 1402393238937 produced the product "product 26" which is the 26th product of the production, Stack.index = 6, curent time = 1402393238937 consumption of product "product 25" This is the product of the 20th product consumption, Stack.index = 6, current time = 1402393238937pop Consumer index = 5, current time = 1402393239609 consumption of the product "product 26" This is the product consumption of the 21st product, Stack.index = 5, current time = 1402393239609pop Consumer index = 4, Curren  T time = 1402393239640 consumed the product "product 20" This is the product of the 22nd product consumption, Stack.index = 4, current time = 1402393239640push Consumer index = 5,  Current time = 1402393239703 produced the product "product 27" This is the product of the production of the 27th product, Stack.index = 5, curent time = 1402393239703push Consumer Index = 6, Current time = 1402393239937 produced the product "product 28" This is the product of the production of the 28th product, Stack.index = 6, curent time = 1402393239937pop Consumer index = 5, current time = 1402393240406 consumes the product "product 28" This is the product of the 23rd product consumption, Stack.index = 5, current time = 1402393240406push Co Nsumer index = 6, current time = 1402393240406 produced the product "product 29" This is the product of the production of the 29th product, Stack.index = 6, curent time = 1402393240406p OP Consumer index = 5, current time = 1402393240875 consumes the product "product 29" This is the product of the 24th product consumption, Stack.index = 5, Current time = 14023932 40875pop Consumer index = 4, current time = 1402393241781 consumes the product "product 27" This is the product of the 25th product consumption, Stack.index = 4, Current time = 14 02393241781pop Consumerindex = 3, current time = 1402393242703 consumes the product "product 15" This is the 26th product of the product consumption, Stack.index = 3, current time = 1402393242703pop con Sumer index = 2, current time = 1402393243468 consumes the product "product 14" This is the product of the 27th product consumption, Stack.index = 2, current time = 1402393243468p OP Consumer index = 1, current time = 1402393243656 consumes the product "product 12" This is the product of the 28th product consumption, Stack.index = 1, current time = 14023932 43656pop Consumer index = 0, current time = 1402393244375 consumes the product "Product 9" This is the product of the 29th product consumption, Stack.index = 0, Current time = 140 2393244375**/

Java Thread Producerandconsumer

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.