Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Producer-Consumer Problem Here, we have a set of p producers and c co

ID: 3865447 • Letter: P

Question

Problem 1: Producer-Consumer Problem Here, we have a set of p producers and c consumers, each running as a single thread. They are synchronized via shared buffer of size b (i.e., it can accommodate b items) Each buffer item contains the following information: Sales Date (DD/MM/YY), store ID (integer), register# (integer), sale amount (float). Each item represents a sales record from a specific cashier register in a particular location of a retail-chain. Thus, each producer reports sales from a specific store location. Each consumer represents an entity that reads sales records and computes sales statistics locally. Each buffer item is consumed by one and-only-one consumer. When all sales records have been read (indicated by a special flag set by another designated thread), each consumer adds its local statistics to the global statistics (in the shred space). It also prints its own local statistics along with its ID. In addition, your main program (parent process) prints the overall (global) statistics. The statistics to be maintained are: Store-wide total sales Month-wise total sales (in all stores) Aggregate sales (all sales together Total time for simulation (from begin to end) Each producer produces records randomly. Assume that the DD field is 1-30, MM is 01- 12, and YY is always 16. Store IDs are in the 1 to p range (where p is the number of producers). The register numbers range from 1-6 for any store. The sale amount in each item can range between 0.50 and 999.99. Each producer generates its record with random data. Run the program until 10,000 items are produced by all producers together Obviously, the number of items produced so far (by all producers) need to be maintained in shared memory. Each producer is assigned a fixed store ID when it is created. It has the following structure:

Explanation / Answer

// Java program to implement solution of producer

// consumer problem.

import java.util.LinkedList;

public class Threadexample

{

    public static void main(String[] args)

                        throws InterruptedException

    {

        // Object of a class that has both produce()

        // and consume() methods

        final PC pc = new PC();

        // Create producer thread

        Thread t1 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.produce();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Create consumer thread

        Thread t2 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.consume();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Start both threads

        t1.start();

        t2.start();

        // t1 finishes before t2

        t1.join();

        t2.join();

    }

    // This class has a list, producer (adds items to list

    // and consumber (removes items).

    public static class PC

    {

        // Create a list shared by producer and consumer

        // Size of list is 2.

        LinkedList<Integer> list = new LinkedList<>();

        int capacity = 2;

        // Function called by producer thread

        public void produce() throws InterruptedException

        {

            int value = 0;

            while (true)

            {

                synchronized (this)

                {

                    // producer thread waits while list

                    // is full

                    while (list.size()==capacity)

                        wait();

                    System.out.println("Producer produced-"

                                                  + value);

                    // to insert the jobs in the list

                    list.add(value++);

                    // notifies the consumer thread that

                    // now it can start consuming

                    notify();

                    // makes the working of program easier

                    // to understand

                    Thread.sleep(1000);

                }

            }

        }

        // Function called by consumer thread

        public void consume() throws InterruptedException

        {

            while (true)

            {

                synchronized (this)

                {

                    // consumer thread waits while list

                    // is empty

                    while (list.size()==0)

                        wait();

                    //to retrive the ifrst job in the list

                    int val = list.removeFirst();

                    System.out.println("Consumer consumed-"

                                                    + val);

                    // Wake up producer thread

                    notify();

                    // and sleep

                    Thread.sleep(1000);

                }

            }

        }

    }

}

// Java program to implement solution of producer

// consumer problem.

import java.util.LinkedList;

public class Threadexample

{

    public static void main(String[] args)

                        throws InterruptedException

    {

        // Object of a class that has both produce()

        // and consume() methods

        final PC pc = new PC();

        // Create producer thread

        Thread t1 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.produce();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Create consumer thread

        Thread t2 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.consume();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Start both threads

        t1.start();

        t2.start();

        // t1 finishes before t2

        t1.join();

        t2.join();

    }

    // This class has a list, producer (adds items to list

    // and consumber (removes items).

    public static class PC

    {

        // Create a list shared by producer and consumer

        // Size of list is 2.

        LinkedList<Integer> list = new LinkedList<>();

        int capacity = 2;

        // Function called by producer thread

        public void produce() throws InterruptedException

        {

            int value = 0;

            while (true)

            {

                synchronized (this)

                {

                    // producer thread waits while list

                    // is full

                    while (list.size()==capacity)

                        wait();

                    System.out.println("Producer produced-"

                                                  + value);

                    // to insert the jobs in the list

                    list.add(value++);

                    // notifies the consumer thread that

                    // now it can start consuming

                    notify();

                    // makes the working of program easier

                    // to understand

                    Thread.sleep(1000);

                }

            }

        }

        // Function called by consumer thread

        public void consume() throws InterruptedException

        {

            while (true)

            {

                synchronized (this)

                {

                    // consumer thread waits while list

                    // is empty

                    while (list.size()==0)

                        wait();

                    //to retrive the ifrst job in the list

                    int val = list.removeFirst();

                    System.out.println("Consumer consumed-"

                                                    + val);

                    // Wake up producer thread

                    notify();

                    // and sleep

                    Thread.sleep(1000);

                }

            }

        }

    }

}