Java Concurrency Implementation five (producer and consumer mode condition implementation)

Source: Internet
Author: User

Package Com.subject01;import Java.util.priorityqueue;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * condition is only present in Java 1.5, It is used instead of the traditional object's Wait (), notify () to implement inter-threading collaboration, compared to wait (), notify () with object, * using Condition's await (), signal () This approach enables inter-threading collaboration to be more secure and efficient.  Condition is an interface, the basic method is the await () and the signal () method, condition relies on the lock interface, generating a condition base code is lock.newcondition () The await () and signal () methods that call condition must be within lock protection, meaning that the await () in Conditon can be used between Lock.lock () and lock.unlock for the wait of object (); signal () in condition corresponds to the Notify () of object, and Signalall () in condition corresponds to the Notifyall () of object. * @author Sun Tao * May 12, 2016 */public class Custormerandproducterbycondition {private int queuesize = ten;p rivate priorityque ue<integer> queue = new Priorityqueue<integer> (queuesize);p rivate lock lock = new Reentrantlock ();p rivate Condition full = lock.newcondition ();p rivate Condition empty = Lock.newcondition (); class Consumer implements runnable{@ Overridepublic void Run () {consume ();} /** * Consumer * @author Suntao * May 12, 2016 */private void consume () {while (true) {Lock.lock (), try {while (queue.size () = = 0) {TR y {System.out.println ("queue empty, wait for data"); Empty.await ();} catch (Interruptedexception e) {e.printstacktrace ()}} Queue.poll (); full.signal (); System.out.println ("Take an element from the queue, queue remaining" +queue.size () + "elements");} Finally{lock.unlock ();}}} /** * consumer * @author Sun Tao * May 12, 2016 */class Producer implements runnable{@Overridepublic void run () {produce ();} private void produce () {while (true) {lock.lock (); try {while (queue.size () = = Queuesize) {try {System.out.println ("queue full, Waiting for free space "); full.await ();} catch (Interruptedexception e) {e.printstacktrace ();}} Queue.offer (1); empty.signal ();} Finally{lock.unlock ();}}} public static void Main (string[] args) {custormerandproducterbycondition cap = new Custormerandproducterbycondition (); Consumer cus = cap.new Consumer (); Producer Pro = Cap.new Producer (); Thread CusT = new Thread (cus); Thread ProT = new Thread (PRO);p Rot.start (); Cust.start ();}} 

Http://www.cnblogs.com/dolphin0520/p/3920385.html

Java Concurrency Implementation five (producer and consumer mode condition implementation)

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.