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

Write a program in Java which creates producer and consumer threads. The produce

ID: 3585927 • Letter: W

Question

Write a program in Java which creates producer and consumer threads. The producer should “produce” by setting the elements of an array of integers to FULL. The consumer should “consume” by setting the elements of an array of integers to EMPTY.

Make sure you properly handle the following situations:

A producer (consumer) producing (consuming) past the end (beginning) of the buffer

A producer writing to an array element that is not EMPTY and a consumer reading (i.e. setting the array element to EMPTY) from an array element that is not FULL.

Use wait() and notify() to avoid errors producing and consuming and to ensure thread synchronization.

Explanation / Answer

import java.util.LinkedList;

public class ThreadClass
{
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final ProducerConsumer pc = new ProducerConsumer();

// 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 consumer (removes items).---eats
public static class ProducerConsumer
{
// 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 -- till producer produces
while (list.size()==0)
wait();

//to retrive the ifrst job in the list
int val = list.removeFirst();

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

// Wake up producer thread to produce items
notify();

// and sleep if stack empty
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