Consumer and producer (multithreading)

Source: Internet
Author: User

1 /**2 * Requirements: Assume that there are 10 consumers to consume the production of products, products to ensure that the order by any other consumer consumption, the last consumption of the next to spend3  * 4  * @authorTrfizeng5  * 6  */7  Public classScthread {8      Public Static voidMain (string[] args) {9SYSTEM.OUT.PRINTLN ("Begin:" + (System.currenttimemillis ())/1000);Ten          for(inti = 0; I < 10; i++) {//unable to move equivalent to producer One             FinalString input = i + "";//can't move . AString output =testdao.dosome (input); -System.out.println (Thread.CurrentThread (). GetName () + ":" +output); -         } the     } -  -     //This class cannot move the equivalent of the consumer -     Static classTestdao { +          Public Staticstring Dosome (String input) { -             Try { +Thread.Sleep (1000); A}Catch(Exception e) { at e.printstacktrace (); -             } -String output = input + ":" + (System.currenttimemillis ()/1000); -             returnoutput; -         } -     } in}
View Code
Solution:
1 ImportJava.util.concurrent.ArrayBlockingQueue;2 ImportJava.util.concurrent.BlockingQueue;3 ImportJava.util.concurrent.locks.Lock;4 ImportJava.util.concurrent.locks.ReentrantLock;5 6 /**7 * Requirements: Assume that there are 10 consumers to consume the production of products, products to ensure that the order by any other consumer consumption, the last consumption of the next to spend8  * 9  * @authorTrfizengTen  *  One  */ A  Public classScthread { -      Public Static voidMain (string[] args) { -         FinalLock lock =NewReentrantlock (); the         FinalBlockingqueue<string> Blockingqueue =NewArrayblockingqueue<string>( -10); -SYSTEM.OUT.PRINTLN ("Begin:" + (System.currenttimemillis ())/1000); -          for(inti = 0; I < 10; i++) {//unable to move equivalent to producer +             FinalString input = i + "";//can't move . -             /* + * String output = testdao.dosome (input); A * SYSTEM.OUT.PRINTLN (Thread.CurrentThread (). GetName () + ":" + at * output); -              */ -             Try { - blockingqueue.put (input); -}Catch(interruptedexception E1) { - e1.printstacktrace (); in             } -         } to  +          for(inti = 0; I < 10; i++) { -             NewThread () { the @Override *                  Public voidrun () { $ Lock.lock ();Panax Notoginseng String output; -                     Try { the                         //synchronized (scthread.class) { +Output =Testdao.dosome (Blockingqueue.take ()); A System.out.println (Thread.CurrentThread (). GetName () the+ ":" +output); +                         // } -}Catch(interruptedexception e) { $ e.printstacktrace (); $}finally { - Lock.unlock (); -                     } the                 } - }.start ();Wuyi         } the     } -  Wu     //This class cannot move the equivalent of the consumer -     Static classTestdao { About          Public Staticstring Dosome (String input) { $             Try { -Thread.Sleep (1000); -}Catch(Exception e) { - e.printstacktrace (); A             } +String output = input + ":" + (System.currenttimemillis ()/1000); the             returnoutput; -         } $     } the}
View Code
Begin:1420089616
Thread-0:0:1420089617
Thread-1:1:1420089618
Thread-3:2:1420089619
Thread-4:3:1420089620
Thread-2:4:1420089621
thread-5:5 :1420089622
thread- 7:6:1420089623
Span class= "Typ" >thread-6:7 :1420089624
thread- 8:8:1420089625
Span class= "Typ" >thread-9:9 :1420089626

Consumer and producer (multithreading)

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.