Producer and consumer model--java design Patterns

Source: Internet
Author: User
Tags volatile

Tag: Wait cache zed while loop row lag stop USG. SH

Producers and consumers:

Just like in the fast food restaurant order meal, there are many dozen rice, there are irregular people to buy rice, buy rice from the fast food restaurant automatically pick meal, if the fast food inventory quantity reaches the lower limit, automatically start to play rice, supplementary bento.

Pass the variable isstop through the while loop to determine if the current while loop is ending.

1  PackageJquery.test.interview;2 3 ImportJava.util.Vector;4 5  Public classProductor<t>ImplementsRunnable {6     7     Private volatileVector<t>v;8     9     Private volatile BooleanFLG =true;Ten     Private volatile BooleanPROCFLG =true; One      A      PublicProductor (vector<t>v) { -           This. v =v; -     } the      -     /** - * Production Products -      * @return +      */ -      Public voidcreateproduct () { +         synchronized(v) { A              while(FLG) { atSystem.out.println ("Number of products in stock ....") 1.. "+v.size ()); -                 if((V.isempty () | | v.size () < 3) &&PROCFLG) { -System.out.println ("Tight inventory, start production ..."); -V.add ((T) "Product1 ..."); -V.add ((T) "Product2 ..."); -V.add ((T) "Product3 ..."); inV.add ((T) "Product4 ..."); -V.add ((T) "Product5 ..."); to                 } +                 //Start Waiting -                 Try { theSystem.out.println ("Number of products in stock ... 2 ... "+v.size ()); * V.notifyall (); $ v.wait ();Panax Notoginseng}Catch(interruptedexception e) { - V.notifyall (); the                 } +             } A             if(!FLG) { theSystem.out.println ("-------------Stop proc---"); + V.notifyall (); -             } $         } $     } -  - @Override the      Public voidrun () { - createproduct ();Wuyi     } the      -      Public voidStop () { WuFLG =false; -     } About      $      Public voidStopproc () { -PROCFLG =false; -     } -  A}
Productor
1  PackageJquery.test.interview;2 3 ImportJava.util.Vector;4 5  Public classCustomer<t>ImplementsRunnable {6     7     Private volatileVector<t>Vector;8     9     Private volatile BooleanFlag2 =true;Ten      One      PublicCustomer (vector<t>vector) { A          This. Vector =Vector; -     } -      the      Public  voidgetproduct () { -         synchronized(vector) { -SYSTEM.OUT.PRINTLN ("-----Stop---customer--222222222-"); -              while(flag2) { +SYSTEM.OUT.PRINTLN ("-----flag2----^^ ^^-"); -                 if(NULL= = Vector | |Vector.isempty ()) { +                     Try { ASystem.out.println ("-No product waiting ..."); at vector.wait (); -}Catch(interruptedexception e) { - e.printstacktrace (); -                     } -}Else{ -System.out.println ("--Get product use ..." + vector.get (0)); inVector.remove (0); - vector.notify (); to                 } +             } -             if(!Flag2) { theSYSTEM.OUT.PRINTLN ("-----Stop---customer---"); * Vector.notifyall (); $             }Panax Notoginseng         } -     } the  + @Override A      Public voidrun () { the getproduct (); +     } -      $      Public voidStop () { $Flag2 =false; -     } -  the}
Customer
1      Public Static voidMain (string[] args)throwsinterruptedexception, executionexception {2Vector<string> Product =NewVector<string>();3Executorservice pool =Executors.newcachedthreadpool ();4productor proc =Newproductor (product);5 Pool.submit (proc);6Productor PROC2 =Newproductor (product);7 Pool.submit (PROC2);8List<customer> Cusgroup =NewArraylist<customer>();9          for(intI =0;i<1;i++){TenCustomer cus =NewCustomer (product); One Cusgroup.add (cus); A pool.submit (cus); -         } -Thread.Sleep (1000*1); theProc.stopproc ();//Termination of production -Proc2.stopproc ();//Termination of production -Cusgroup.foreach (Cus-cus.stop ());//Close Customer -Proc.stop ();//Close the producer, (Close the consumer first, or the consumer cannot close it) +Proc2.stop ();//Close the producer, (Close the consumer first, or the consumer cannot close it) -Thread.Sleep (1000*2); + Pool.shutdown (); ASystem.out.println ("-------------End"); at}
Main

Producer and consumer model--java design Patterns

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.