Saturday, 16 May 2015

Low-Level Producer-Consumer ( wait, notify and synchronized)

//****************************************************************
//* Copyright (c) 2015 . All Rights Reserved.
//****************************************************************
package com.java.thread.producerconsumer;

import java.util.LinkedList;
import java.util.Random;

public class ProducerConsumerWaitNotify {
    private LinkedList<Integer> list = new LinkedList<Integer>();
    private final int LIMIT = 10;
    private Object lock = new Object();

    public void produce() throws InterruptedException {

        int value = 0;

        while (true) {

            synchronized (lock) {

                while (list.size() == LIMIT) {
                    lock.wait();
                }

                list.add(value++);
                lock.notify();
            }

        }
    }

    public void consume() throws InterruptedException {

        final Random random = new Random();

        while (true) {

            synchronized (lock) {

                while (list.size() == 0) {
                    lock.wait();
                }

                System.out.print("List size is: " + list.size());
                final int value = list.removeFirst();
                System.out.println("; value is: " + value);
                lock.notify();
            }

            Thread.sleep(random.nextInt(1000));
        }
    }

    public static void main(final String[] args) {
        final ProducerConsumerWaitNotify producerConsumerWaitNotify =
                new ProducerConsumerWaitNotify();
        producerConsumerWaitNotify.main();
    }

    public void main() {
        final Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    produce();
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        final Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    consume();
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();

    }
}

Output:

List size is: 2; value is: 0
List size is: 10; value is: 1
List size is: 10; value is: 2
List size is: 10; value is: 3
List size is: 10; value is: 4
List size is: 10; value is: 5
List size is: 10; value is: 6
List size is: 10; value is: 7
List size is: 10; value is: 8
List size is: 10; value is: 9
List size is: 10; value is: 10
List size is: 10; value is: 11
List size is: 10; value is: 12
List size is: 10; value is: 13
List size is: 10; value is: 14
List size is: 10; value is: 15
List size is: 10; value is: 16
List size is: 10; value is: 17
List size is: 10; value is: 18
List size is: 10; value is: 19

List size is: 10; value is: 20


Kindly provide your valuable feedback in comment box.

No comments:

Post a Comment