Java implementation of the producer consumer Model (Implementation three)

Source: Internet
Author: User

Exchanger is a concurrency tool under the Java.util.concurrent class library. The following is a description of exchanger in the Java API documentation.

A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object in entry to the Exchange method, matches with a partner thread, and receives its partner ' S object on return. An Exchanger is viewed as a bidirectional form of a synchronousqueue. Exchangers May is useful in applications such as genetic algorithms and pipeline designs.

That is, exchanger must be accompanied by a pair of threads appearing.

 Packagecom.smikevon.concurrent;ImportJava.util.Random;ImportJava.util.concurrent.Exchanger;ImportJava.util.concurrent.Executor;Importjava.util.concurrent.Executors;/*** @description: The producer consumer model uses exchanger to achieve* @date: September 18, 2014 PM 4:28:49*/ Public classproducerconsumer_02 {Static classProducerImplementsRunnable {PrivateExchanger<string>Exchanger; PrivateString name;  PublicProducer (exchanger<string>exchanger,string name) {             This. Exchanger =Exchanger;  This. Name =name; }         Public voidrun () {string[] messages= {                "I Am",                "A programmer",                "I'm very proud.",                "Also very proud",                "Dedication to love and hillock",                "Diligence and Dedication",                No regrets,                "Dedication to Youth",                "Hope by learning",                Improve,                Own,                "Done",            }; Try {                 for(inti=0;i<messages.length;i++) {System.out.format ("%s: Put information-----------%s%n", Name,messages[i]);                    Exchanger.exchange (Messages[i]); Thread.Sleep (NewRandom (System.currenttimemillis ()). Nextint (5000)); }            } Catch(interruptedexception e) {e.printstacktrace (); }        }    }    Static classConsumerImplementsrunnable{PrivateExchanger<string>Exchanger; PrivateString name;  PublicConsumer (exchanger<string>exchanger,string name) {             This. Exchanger =Exchanger;  This. Name =name; }         Public voidrun () {Try{String message= "";  Do{message= Exchanger.exchange (""); System.out.format ("%s: Remove information-------------%s%n", Name,message); Thread.Sleep (NewRandom (System.currenttimemillis ()). Nextint (5000)); } while(!message.equals ("Done")); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }     Public Static voidMain (string[] args) {Executor Executor=Executors.newcachedthreadpool (); Exchanger<String> exchanger =NewExchanger<string>(); Executor.execute (NewProducer (Exchanger, "producer")); Executor.execute (NewConsumer (Exchanger, "consumer")); }}

Java implementation of the producer consumer Model (Implementation three)

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.