Question

In: Computer Science

Java Code The producer and consumer will share an integer array with a length of 5...

Java Code

The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer.

The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to five seconds (compute a new random wait value on each pass through the loop). The loop is to be executed 100 times. Each time through the loop, it places the loop count into the shared circular buffer if there is an available slot in the buffer, and updates the variable(s) used to control the use of the circular buffer (note that this will have to rap around as the array is only 5 items long). The producer must not clobber any item in the buffer that has not been read by the consumer. If the list is full, the producer must wait for the consumer to remove one or more items from the list before placing the next item in it. This wait is inside the main loop and must have a 1 second sleep in it. The buffer may get full because the producer may be faster than the consumer at times. When the producer puts its last entry into the buffer, it must add one more entry, a -1 which indicates to the consumer that it has completed.

The consumer process consists of a loop that reads from the shared circular buffer until the “I’m done” flag (the -1) is detected in the shared buffer indicating that the transfer is complete. On each pass through the loop, before it reads from the shared buffer, the consumer does a random wait of from two to five seconds (compute a new random value on each pass through the loop). It will only read a value from the shared circular buffer if there is one in the buffer it hasn’t read. If there is nothing to read, it must wait until there is something to read. When it has to wait it is to write “consumer waiting” into the output file. When waiting it should do so in a loop that has a 1 second sleep in it and write the “consumer waiting” message into the file each pass through this wait loop. The consumer must use the variable(s) it shares with the producer to determine which item in the circular buffer is the next item (remember the shared buffer is circular). After the consumer reads a value from the shared circular buffer, it writes the value into an output file and updates the variable(s) it shares with the producer (again note that this has to take into consideration the fact that this index will rap abound) to indicate that this entry is now available for use as the entry has been processed. When it completes, the consumer writes the phrase “Consumer done” into the output file. Note that the entry just before this should be 99 (the last item removed from the circular buffer). Do not write the -1 into the output buffer.

Solutions

Expert Solution

import java.util.concurrent.Semaphore;

  class Q {

    int item;

    static Semaphore semCon = new Semaphore(0);

    static Semaphore semProd = new Semaphore(1);

  

    // to get an item from buffer

    void get()

    {

        try {

            // Before consumer can consume an item,

            // it must acquire a permit from semCon

            semCon.acquire();

        }

        catch (InterruptedException e) {

            System.out.println("InterruptedException caught");

        }

  

        // consumer consuming an item

        System.out.println("Consumer consumed item : " + item);

  

        // After consumer consumes the item,

        // it releases semProd to notify producer

        semProd.release();

    }

  

    // to put an item in buffer

    void put(int item)

    {

        try {

            // Before producer can produce an item,

            // it must acquire a permit from semProd

            semProd.acquire();

        }

        catch (InterruptedException e) {

            System.out.println("InterruptedException caught");

        }

  

        // producer producing an item

        this.item = item;

  

        System.out.println("Producer produced item : " + item);

  

        // After producer produces the item,

        // it releases semCon to notify consumer

        semCon.release();

    }

}

  

// Producer class

class Producer implements Runnable {

    Q q;

    Producer(Q q)

    {

        this.q = q;

        new Thread(this, "Producer").start();

    }

  

    public void run()

    {

        for (int i = 0; i < 5; i++)

            // producer put items

            q.put(i);

    }

}

  

// Consumer class

class Consumer implements Runnable {

    Q q;

    Consumer(Q q)

    {

        this.q = q;

        new Thread(this, "Consumer").start();

    }

  

    public void run()

    {

        for (int i = 0; i < 5; i++)

            // consumer get items

            q.get();

    }

}

  

// Driver class

class PC {

    public static void main(String args[])

    {

        // creating buffer queue

        Q q = new Q();

  

        // starting consumer thread

        new Consumer(q);

  

        // starting producer thread

        new Producer(q);

    }

}


Related Solutions

Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined length
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined lengthJAVA
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT