Java Producer Consumer Model using Threads

Here is a simple example to illustrate Producer - Consumer Model using Java Threads.

public class Producer implements Runnable
{

        private static int      count   = 0;

        @Override
        public void run()
        {
                while (true)
                {
                        synchronized (Market.productList)
                        {

                                while (!(Market.productList.size() >= 0))
                                {
                                        System.out.println("entered while" + Market.productList.size());
                                        try
                                        {
                                                Market.productList.wait();
                                        }
                                        catch (InterruptedException e)
                                        {
                                                e.printStackTrace();
                                        }
                                }

                                System.out.println("adding product to market --> " + count);
                                Market.productList.add("product" + count++);
                                Market.productList.notify();
                        }

                        try
                        {
                                Thread.sleep(10000);
                        }
                        catch (InterruptedException e)
                        {
                                e.printStackTrace();
                        }
                }
        }

}

public class Consumer implements Runnable
{

        @Override
        public void run()
        {
                while (true)
                {

                        synchronized (Market.productList)
                        {

                                while (Market.productList.size() == 0)
                                {
                                        System.out.println("Consumer waiting..");
                                        try
                                        {
                                                Market.productList.wait();
                                        }
                                        catch (InterruptedException e)
                                        {
                                                e.printStackTrace();
                                        }
                                }

                                int s = Market.productList.size();
                                for (int i = 0; i < s; i++)
                                {
                                        System.out.println("product to be consumed --> " + Market.productList.get(0));
                                        Market.productList.remove(0);
                                }
                                Market.productList.notify();
                        }

                        try
                        {
                                Thread.sleep(10000);
                        }
                        catch (InterruptedException e)
                        {
                                e.printStackTrace();
                        }

                }

        }

}

import java.util.ArrayList;
import java.util.List;

public class Market
{
        public static List<String>      productList     = new ArrayList<String>();

        public static void main(String[] args)
        {
                Producer producer = new Producer();

                Thread producerThread = new Thread(producer);
                producerThread.start();

                try
                {
                        Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                        e.printStackTrace();
                }

                Consumer consumer = new Consumer();

                Thread consumerThread = new Thread(consumer);
                consumerThread.start();

                try
                {
                        producerThread.join();
                        consumerThread.join();
                }
                catch (InterruptedException e1)
                {
                        e1.printStackTrace();
                }
        }
}

Technology: 

Search