I have written code for producer consumer problem. Below is the code
package sample;
import java.util.List;
import java.util.ArrayList;
public class Interview2 {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<Employee>();
Thread producer = new Thread(new Producer(empList , 4) , "Producer");
Thread consumer = new Thread(new Consumer(empList , 4) , "Consumer");
producer.start();
consumer.start();
}
}
class Employee
{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Producer implements Runnable
{
List<Employee> empList;
int size;
public Producer(final List<Employee> empList , final int size)
{
this.empList = empList;
this.size = size;
}
public void run() {
for(int i=0; i<7;i++)
{
System.out.println("Produced "+i);
try {
produce(new Employee());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void produce(Employee e) throws InterruptedException
{
while(empList.size()==size) // If list is full then will have to wait
{
synchronized(empList)
{
System.out.println("List is full "+Thread.currentThread().getName()+" Is waiting and" + " Size is "+empList.size());
empList.wait();
}
}
synchronized(empList)
{
empList.add(e);
empList.notify();
}
}
}
class Consumer implements Runnable
{
List<Employee> empList;
int size;
public Consumer(final List<Employee> empList , final int size)
{
this.empList = empList;
this.size = size;
}
public void run()
{
while(true)
{
try {
System.out.println("Consumed ");
Thread.sleep(50);
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void consume() throws InterruptedException
{
while(empList.isEmpty()) // If list is empty then will have to wait
{
synchronized(empList)
{
System.out.println("List is empty "+Thread.currentThread().getName()+" Is waiting and " + "Size is "+empList.size());
empList.wait();
}
}
synchronized(empList)
{
empList.remove(0);
empList.notifyAll();
}
}
}
But I want this code to be like producer adds one employee in list after that consumer consumes it means there must be switch between producer and consumer. I want to repeat it for 10 objects. Kindly help me to modify the code. Thanks in advance
Since this looks like some kind of an assignment, I am going to point you in the right direction instead of providing the code itself :
You can use an ArrayBlockingQueue with size 1 instead of an ArrayList
:
ArrayBlockingQueue<Employee> empList = new ArrayBlockingQueue<Employee>(1);
The ArrayBlockingQueue
class provides two blocking calls namely put and take. By using these methods, you can get rid of any explicit thread communication that you are currently doing through wait
and notify
. The ideal way to do this would be to call the put
method in your Producer
thread and the take
method in your Consumer
thread in a while
loop.
Since the queue is initialized with a size of 1, any thread that tries to insert a new element in the queue when it is full will wait when put
is called in the while loop. Similarly, any thread trying to get an element from the queue when it is empty will have to wait when take
is called in the while loop.