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

>>>Code IN C++<<< NOT in C Modify “Producer and Consumer Problem” from lecture n

ID: 3847667 • Letter: #

Question

>>>Code IN C++<<< NOT in C
Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows:

1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through random number generator function and inserts this into buffer and prints the number. Increment counter. 4. Consumer thread goes to the buffer and takes a number in the proper order and prints it out. Increment counter. 5. After counter reaches its limit, both threads should be terminated and return to main. 6. Main program terminates. 7. You can implement this project in any OS environment of your choice, windows, linux, etc. Also you can use any programming languages you want but your program and sample run should clear show that you implemented subtasks using separate threads. >>>Code IN C++<<< NOT in C
Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows:

1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through random number generator function and inserts this into buffer and prints the number. Increment counter. 4. Consumer thread goes to the buffer and takes a number in the proper order and prints it out. Increment counter. 5. After counter reaches its limit, both threads should be terminated and return to main. 6. Main program terminates. 7. You can implement this project in any OS environment of your choice, windows, linux, etc. Also you can use any programming languages you want but your program and sample run should clear show that you implemented subtasks using separate threads. >>>Code IN C++<<< NOT in C
Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows:

1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through random number generator function and inserts this into buffer and prints the number. Increment counter. 4. Consumer thread goes to the buffer and takes a number in the proper order and prints it out. Increment counter. 5. After counter reaches its limit, both threads should be terminated and return to main. 6. Main program terminates. 7. You can implement this project in any OS environment of your choice, windows, linux, etc. Also you can use any programming languages you want but your program and sample run should clear show that you implemented subtasks using separate threads.

Explanation / Answer

import java.util.LinkedList;

public class ProducerConsumerUpdate
{
public static void main(String[] args)
throws InterruptedException
{

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();
}

  
public static class PC
{

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)
{
  
while (list.size()==capacity)
wait();

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

notify();

Thread.sleep(1000);
}
}
}

// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
  
while (list.size()==0)
wait();

  
int val = list.removeFirst();

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

// Wake up producer thread
notify();

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote