Java language synchronized and lock realize producer consumer __java

Source: Internet
Author: User

Synchronized Way

Package test;
Import java.util.LinkedList;
Import Java.util.Queue;

Import Java.util.Random; public class Producerconsumer {public static void main (String args[]) {queue<integer> buffer = new L 
        Inkedlist<> (); 
        int maxSize = 10;
        Producerconsumer Producerconsumer = new Producerconsumer (); 
        Thread producer = Producerconsumer.new producer (buffer, MaxSize, "producer"); 
        Thread consumer = producerconsumer.new consumer (buffer, maxSize, "consumer"); 
        Producer.start ();  
    Consumer.start ();
        Class Producer extends thread{private queue<integer> Queue;
        private int maxSize;
            Public Producer (queue<integer> queue,int maxsize,string name) {super (name);
            This.queue = queue;
        This.maxsize = maxSize;
                    @Override public void Run () {while (true) {synchronized (queue) { while (QueuE.size () = = MaxSize) {System.out.println ("Queue is full");
                        try {queue.wait ();
                        catch (Interruptedexception e) {e.printstacktrace ();
                    } Random Random = new Random ();
                    int i = Random.nextint (); 
                    System.out.println ("Producing value:" + i); 
                    Queue.add (i);
                Queue.notifyall (); 
        Class Consumer extends thread{private queue<integer> Queue; 
        private int maxSize; 
            Public Consumer (queue<integer> Queue, int maxSize, String name) {super (name); 
            This.queue = queue; 
        This.maxsize = maxSize; @Override public void Run () {while (true) {SynchronizeD (queue) {while (Queue.isempty ()) {System.out.println (' queue is empty '); 
                        try {queue.wait (); 
                        catch (Exception ex) {ex.printstacktrace (); 
                    } System.out.println ("Consuming value:" + queue.remove ()); 
                Queue.notifyall (); } 
            } 
        } 
    }
}

Lock Method

Package test;
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.Random;
Import java.util.concurrent.locks.Condition;
Import Java.util.concurrent.locks.Lock;

Import Java.util.concurrent.locks.ReentrantLock;
    public class Lockproducerconsumer {lock lock = new Reentrantlock ();
    Condition Conditionp = Lock.newcondition ();
    Condition Conditionc = Lock.newcondition (); 
        public static void Main (String args[]) {queue<integer> buffer = new linkedlist<> (); 
        int maxSize = 10;
        Lockproducerconsumer Producerconsumer = new Lockproducerconsumer (); 
        Thread producer = Producerconsumer.new producer (buffer, MaxSize, "producer"); 
        Thread consumer = producerconsumer.new Consumer (buffer, "consumer"); 
        Producer.start ();  
    Consumer.start ();
        Class Producer extends thread{private queue<integer> Queue;
        private int maxSize; Public Producer (queue<integer> queue,inT maxsize,string name) {super (name);
            This.queue = queue;
        This.maxsize = maxSize;
                @Override public void Run () {while (true) {Lock.lock (); 
                            try {while (true) {while (queue.size () = = MaxSize) {
                            System.out.println ("Queue is full");
                            try {conditionp.await ();
                            catch (Interruptedexception e) {e.printstacktrace ();
                        } Random Random = new Random ();
                        int i = Random.nextint (); 
                        System.out.println ("Producing value:" + i);
                        Queue.add (i);


                    Conditionc.signal ();
                    }}finally {Lock.unlock (); 
        Class Consumer extends thread{private queue<integer> Queue; 
            Public Consumer (queue<integer> Queue, String name) {super (name);
        This.queue = queue;
            @Override public void Run () {lock.lock (); try {while (true) {while (Queue.isempty ()) {SYSTEM.OUT.P 
                        Rintln ("Queue is empty");
                        try {conditionc.await ();
                        catch (Interruptedexception e) {e.printstacktrace (); 
                    } System.out.println ("Consuming value:" + queue.remove ());

                Conditionp.signal ();
            }}finally {Lock.unlock ();
 }

        } 
    }
}

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.