Multithread/Queue Implement a Queue class whose add and remove methods are synchro- nized. Supply one thread, called
Question:
Multithread/Queue Implement a Queue class whose add and remove methods are synchro- nized. Supply one thread, called the producer, which keeps inserting strings into the queue as long as there are fewer than ten elements in it. When the queue gets too full, the thread waits. As sample strings, simply use time stamps new Date().toString(). Supply a second thread, called the consumer, that keeps removing and printing strings from the queue as long as the queue is not empty. When the queue is empty, the thread waits. Both the consumer and producer threads should run for 100 iterations.
Given Codes:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.ArrayList;
/**
This class implements the FIFO queue data structure.
*/
public class Queue
{
private ArrayList queue;
private int size = 0;
private static final int DEAFULT_SIZE = 10;
private Lock queueLock;
private Condition queueChangeCondition;
/**
Constructs the maximum size of the queue to default size.
*/
public Queue()
{
//complete this method
}
/**
Constructs the maximum size of the queue to the given size.
@param aSize the maximum size of the queue
*/
public Queue(int aSize)
{
//complete this method
}
/**
Adds a string into the queue.
@param item the item to add
*/
public void add(String item) throws InterruptedException
{
//complete this method
}
/**
Removes one item from the queue.
@return the first item of the queue
*/
public String remove() throws InterruptedException
{
//complete this method
}
/**
Check if the queue is empty.
@return true if the queue is empty, else false
*/
public boolean isEmpty()
{
return queue.isEmpty();
}
/**
Check if the queue is full.
@return true if queue size equals to maximum size, else false
*/
public boolean isFull()
{
return queue.size() == size;
}
/**
This class will keep removing things from the queue
*/
public class ConsumerRunnable implements Runnable
{
private Queue queue = null;
private int count;
/**
Constructs the consumer with a queue and count.
@param aQueue the queue that the consumer going to consume from
@param aCount the number of time that consumer going to consume
*/
public ConsumerRunnable(Queue aQueue, int aCount)
{
count = aCount;
queue = aQueue;
}
public void run()
{
. . .
for (. . .)
{
. . .
System.out.println("Removing: " + item);
. . .
}
. . .
}
}
import java.util.Date;
/**
This class will keep putting things into the queue.
*/
public class ProducerRunnable implements Runnable
{
private Queue queue = null;
private int count;
/**
Constructs the producer with a queue and count.
@param aQueue the queue that the consumer going to consume from
@param aCount the number of time that consumer going to consume
*/
public ProducerRunnable(Queue aQueue, int aCount)
{
count = aCount;
queue = aQueue;
}
public void run()
{
. . .
for (. . .)
{
String item = new Date().toString();
System.out.println("Adding: " + item);
queue.add(item);
. . .
}
. . .
}
}
public class ProducerConsumerRunner
{
private static int QUEUE_MAX_SIZE = 10;
private static int ITERATIONS = 100;
public static void main(String args[])
{
Queue q = new Queue(QUEUE_MAX_SIZE);
ProducerRunnable producer = new ProducerRunnable(q, ITERATIONS);
ConsumerRunnable consumer = new ConsumerRunnable(q, ITERATIONS);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
Data Structures and Algorithm Analysis in Java
ISBN: 978-0132576277
3rd edition
Authors: Mark A. Weiss