Question

In: Computer Science

Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...

Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below.

This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer 20 10 1”. The code must use threads, mutexes, and semaphores. Note that the number of producer and consumer threads does not have to be equal. The ProducerConsumer application will create the specified number of producer and consumer threads and then sleep letting both producer(s) and consumer(s) place 100 random integers into a bounded buffer that has a finite number of slots to hold data. Each producer and consumer thread will also choose a random amount of time to sleep using Random() (make sure to look up what kind of random number Random() provides) and Thread.sleep() that is less than the given sleep time as it places 100 random integers into this one bounded buffer. Set the sleep time of each producer and consumer thread to be a random amount of time between 0 to 0.5 seconds because we will test it over a 20-second period. Each producer and consumer thread will print the random number that it placed in the bounded buffer or removed from the bounded buffer to the screen. Set the bounded buffer equal to five.  

The producer will 100 times go through the cycle of sleeping a random amount of time, creating a random integer, inserting it into the bounded buffer of size 5, and then printing the random integer.

The consumer will 100 times go through the cycle of sleeping a random amount of time, consuming an item from the bounded buffer, and then printing the random integer.

Use a mutex variable named mutex to protect the buffer and counting semaphores named empty and full so that a consumer does not try to remove an item from an empty buffer (empty) and a producer does not try to place an item into a full buffer (full). At the beginning initialize empty = 5 and full = 0 before creating the producer and consumer threads. Below is the usage and an example of executing this application.

Usage.

java ProducerConsumer <sleep time> <producer threads> <consumer threads>

The following command creates 5 producer threads and 1 consumer thread and lets them run for 20 seconds. Notice the pattern!

$ java ProducerConsumer 20 5 1

Using arguments from command line

Sleep time = 20

Producer threads = 5

Consumer threads = 1

Producer produced 6595

        Consumer consumed 6595

Producer produced 97415

        Consumer consumed 97415

Producer produced 50798

Producer produced 25626

Producer produced 95610

Producer produced 88100

Producer produced 14130

        Consumer consumed 50798

Producer produced 95845

        Consumer consumed 25626

Producer produced 71052

        Consumer consumed 95610

Producer produced 64862

        Consumer consumed 88100

Producer produced 26986

        Consumer consumed 14130

Producer produced 17117

        Consumer consumed 95845

Producer produced 33333

        Consumer consumed 71052

Producer produced 28460

        Consumer consumed 64862

Producer produced 11266

        Consumer consumed 26986

Producer produced 46761

        Consumer consumed 17117

Solutions

Expert Solution

import java.util.LinkedList;

public class Threadexample
{
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();

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

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

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

t1.join();
t2.join();
}

public static class PC
{
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
while (list.size()==capacity)
wait();

System.out.println("Producer produced-"
+ value);

list.add(value++);

notify();

Thread.sleep(1000);
}
}
}

public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
while (list.size()==0)
wait();

int val = list.removeFirst();

System.out.println("Consumer consumed-"
+ val);

notify();

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

Thanks...


Related Solutions

The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to a shared buffer and a consumer process consumes items from that buffer (in the first-in-first-out (FIFO) order). When a producer adds a new item, the buffer size grows by one, and whenever a consumer consumes an item from the buffer, the size is reduced by one. The producer process must wait when the buffer is full likewise the consumer process must wait when the...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design...
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design in C book. All input should be read from a file and output should be written to a file. Make sure that you design a function greatest_common_divisor() which calculates and returns the greatest common divisor of two integer numbers. Also, develop functions to read data from a file and to write data to a file. Problem Statement: The greatest common divisor (gcd) of two...
A) Describe and explain in detail the producer-consumer principle for concurrent programming as we studied in...
A) Describe and explain in detail the producer-consumer principle for concurrent programming as we studied in this course B) Choose one of the mechanisms we use studied to synchronize the producer and consumer threads and write and example demonstrator (5pts) C) Explain in your comments in the Java code the most important aspects that enabled synchronization between the the 2 threads in (ii) above. (5pts)
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Using Supply and Demand to Analyze Markets — End of Chapter Problem The Reinheitsgebot is a...
Using Supply and Demand to Analyze Markets — End of Chapter Problem The Reinheitsgebot is a set of laws established in the 1500s that regulate the production and sale of beer in Germany. Among its provisions, the edict set maximum prices that brewers could charge at various times of the year: "From Michaelmas to Georgi, the price for one [Bavarian Liter] is not to exceed one Pfennig Munich value." Cheap beer — this must be a great thing for consumers....
Using Supply and Demand to Analyze Markets — End of Chapter Problem The Reinheitsgebot is a...
Using Supply and Demand to Analyze Markets — End of Chapter Problem The Reinheitsgebot is a set of laws established in the 1500s that regulate the production and sale of beer in Germany. Among its provisions, the edict set maximum prices that brewers could charge at various times of the year: "From Michaelmas to Georgi, the price for one [Bavarian Liter] is not to exceed one Pfennig Munich value." Cheap beer — this must be a great thing for consumers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT