Notes [Java7 Concurrent Programming Manual]2.4 use conditions in synchronous code-producers and consumers

Source: Internet
Author: User

Description

A typical problem in concurrent programming is producer-consumer issues. In the program, it is possible to use two threads of communication, such as the producer consumer, to obtain a shared data, there is consumption. There is no waiting for producers to continue spending after production. Then this implementation process can use Wait (); notify (); Notifyall () to achieve the effect;
Detailed explanation of the above methods please see: Java Multithreading Series – "Basics" 05 thread wait and wake up

Example
/** * Created by Zhuqiang on 2015/8/8 0008. * * Public  class Client {     Public Static void Main(string[] args) {Storage St =NewStorage ();NewThread (NewProducer (ST),"Little Red"). Start ();NewThread (NewConsumer (ST),"nickname"). Start (); }}/** * Warehouse * *Class Storage {Private intMaxSize =Ten;//Warehouse Maximum capacity    PrivateLinkedlist<date> st =NewLinkedlist<date> ();//Warehouse storage    /** Production * /     Public synchronized void producer() { while(St.size () > MaxSize) {Try{System.out.println (Thread.CurrentThread (). GetName () +": The warehouse is full, production is waiting for production."); This. Wait (); }Catch(Interruptedexception e)            {E.printstacktrace (); }} st.push (NewDate ()); System.out.println (Thread.CurrentThread (). GetName () +": Production of a Commodity"); This. Notifyall ();//Wake up other waiting threads}/** Consumption * *     Public synchronized void Consumer() { while(st.size () = =0) {//Pay attention here, come in once, have to cycle judgment if there has been no inventory, wait until the Notifyall () is the thread that wakes up waiting on this monitor lock, it is possible to grab a resource or the current thread            Try{System.out.println (Thread.CurrentThread (). GetName () +": Waiting item: Current item Quantity:"+ st.size ()); This. Wait (); }Catch(Interruptedexception e)            {E.printstacktrace (); }} System.out.println (Thread.CurrentThread (). GetName () +": Consumer goods:"+ St.pop ()); This. Notifyall ();//Wake Up other waiting threads (wake-up producer)} Public int getmaxsize() {returnMaxSize; } Public void setmaxsize(intMaxSize) { This. maxSize = maxSize; } PublicLinkedlist<date>getst() {returnQty } Public void setst(Linkedlist<date> St) { This. st = St; }}/** producer * /Class Producer implements Runnable {PrivateStorage St; Public Producer(Storage St) { This. st = St; }@Override     Public void Run() {/*for (int i = 1;i <=50;i++) */         while(true) {st.producer (); }    }}/** Consumer * *Class Consumer implements Runnable {PrivateStorage St; Public Consumer(Storage St) { This. st = St; }@Override     Public void Run() {/*for (int i = 1;i <=50;i++) */    //Here can not use the loop how many times to simulate, otherwise it will appear (suspended animation lock), if the number of cycles of the producers cycle, then the number of cycles of consumers have not finished cycle, and no goods, then consumers have been waiting. No one wakes up.         while(true) {St.consumer (); }    }}

Description
The above example is roughly logical: producers continue to produce to the warehouse, the warehouse capacity is 10, to reach the maximum capacity of the suspension of production;
Consumers, the constant consumption of goods in the warehouse when they can consume, otherwise wait;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Notes [Java7 Concurrent Programming Manual]2.4 use conditions in synchronous code-producers and consumers

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.