[Java Basics] Java multithreading about consumers and producers

Source: Internet
Author: User
Tags stub

Multithreading: Production and consumption
1. Producers producer produce produce products and put them in stock inventory, while consumers consumer to consume inventory products from inventory consume.
2. The total amount of inventory inventory (maximum stock 100) is limited. If the inventory inventory full, the producer can not continue to produce produce products in the inventory inventory, must wait for the status. Waiting for the product by consumers consumer consumption consume, and then to inventory inventory production produce products.
3. If the inventory inventory empty, the consumer consumer also must wait for the status. Waiting for producers to produce produce products, and then consumer consumer wake up for consumption consume.
1. Stock is 0 ; 2. Consumption 1 consume 10 (but in stock 0, 3. Production 2 production of 5; 4. Production 3 production 5 (at this time the inventory has 10, then meet the consumption of 1), the final inventory is still 0; 5. Production 4 production 100) 6. Production 5 production 10 (because the inventory is full, unable to produce, wait)
ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.ExecutorService;ImportJava.util.concurrent.Executors;ImportJava.util.concurrent.LinkedBlockingQueue;PublicClassblockingqueuetest {PublicStaticvoidMain (string[] args) {Executorservice service =Executors.newcachedthreadpool (); Blockingqueue<string> Blockingqueue =New Linkedblockingqueue<string> (100); System.out.println ("Blockingqueue now contains" + blockingqueue.size () + "unit"); Service.submit (NewConsumer1 (Blockingqueue)); Gap (Blockingqueue); Service.submit (NewProductor2 (Blockingqueue)); Gap (Blockingqueue); Service.submit (NewProductor3 (Blockingqueue)); Gap (Blockingqueue); Service.submit (NewProductor4 (Blockingqueue)); Gap (Blockingqueue); Service.submit (NewProductor5 (Blockingqueue)); Gap (Blockingqueue); Service.shutdown (); }PrivateStaticvoid Gap (blockingqueue<string>Blockingqueue) {Try{Thread.Sleep (1000); }Catch(Interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Blockingqueue now contains" + blockingqueue.size () + "unit"); }}Class Consumer1Implementsrunnable{blockingqueue<string>Blockingqueue;Public Consumer1 (blockingqueue<string>Blockingqueue) {Super();This.blockingqueue =Blockingqueue; } @OverridePublicvoidRun () {//TODO auto-generated Method Stub System.out.println ("Consumer1 start:need units");Forint i = 0; I < 10; i++){Try{Blockingqueue.take ();}Catch(Interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("Consumer1 End:has got units"); } }Class Productor2Implementsrunnable{blockingqueue<string>Blockingqueue;Public Productor2 (blockingqueue<string>Blockingqueue) {Super();This.blockingqueue =Blockingqueue; } @OverridePublicvoidRun () {//TODO auto-generated Method Stub System.out.println ("Productor2 start:put 5 units");Forint i = 0; I < 5; i++){Try{Blockingqueue.put ("Object"); }Catch(Interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("Productor2 End:has put 5 units"); } }Class Productor3Implementsrunnable{blockingqueue<string>Blockingqueue;Public Productor3 (blockingqueue<string>Blockingqueue) {Super();This.blockingqueue =Blockingqueue; } @OverridePublicvoidRun () {//TODO auto-generated Method Stub System.out.println ("Productor3 start:put 5 units");Forint i = 0; I < 5; i++){Try{Blockingqueue.put ("Object"); }Catch(Interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("Productor3 End:has put 5 units"); } }Class Productor4Implementsrunnable{blockingqueue<string>Blockingqueue;Public Productor4 (blockingqueue<string>Blockingqueue) {Super();This.blockingqueue =Blockingqueue; } @OverridePublicvoidRun () {//TODO auto-generated Method Stub System.out.println ("Productor4 start:put units");Forint i = 0; I < 100; i++){Try{Blockingqueue.put ("Object"); }Catch(Interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("Productor4 End:has put units"); } }Class Productor5Implementsrunnable{blockingqueue<string>Blockingqueue;Public Productor5 (blockingqueue<string>Blockingqueue) {Super();This.blockingqueue =Blockingqueue; } @Overridepublic void run () { Span style= "color: #008000;" >// TODO auto-generated method stub System.out.println ("Productor5 start:put" Span style= "color: #000000;" >); for (int i = 0; i <; i++ trycatch (Interruptedexception e) {// TODO auto-generated catch Block E.printstacktrace () ; }} System.out.println ("Productor5 End:has put units"     

Each thread is started by 1s, and the result

Blockingqueue now contains 0 unit Consumer1 start:need units Blockingqueue now contains 0 unit Productor2 start:put 5 Units Productor2 End:has put 5 units Blockingqueue now contains 0 unit Productor3 start:put 5 units Productor3 End:has Put 5 units Consumer1 End:has got units Blockingqueue now contains 0 unit PRODUCTOR4 start:put [units Productor4] End:has put units blockingqueue now contains unit Productor5 start:put ten units blockingqueue now contains u Nit

[Java Basics] Java multithreading about consumers and producers

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.