Question: ASSIGNMENT You are asked to implement a message queue connector class using Java. The following figure depicts a message queue connector class, followed by the
ASSIGNMENT
You are asked to implement a message queue connector class using Java. The following figure depicts a message queue connector class, followed by the detailed specifications of operations. The message queue connector class has send() and receive(), and the size of queue is 3. To test your implementation, your program should create two separate threads, a producer thread and a consumer thread. The producer thread sends a message having a structure, (string, integer) e.g., (add, 3) or (multiply, 7) - to a consumer thread via a message queue connector. There is a SimpleCalculation class that has two operations, add() and multiply(). You should implement the SimpleCalculation class. When the consumer thread receives a message from the connector, it extracts the message and then calls one of operations on the SimpleCalculation class depending on the message. For example, the consumer thread calls the add() operation if it receives a message like (add, 3), while it calls the multiply() operation if it receives a message like (multiply, 3). The add() operation in the SimpleCalculation adds 10 to the integer in a message from the producer thread and displays the result on the screen. The multiply() operation multiplies the integer by 10 and displays the result on the screen. The producer thread should displays the messages on the screen before it sends the messages to the consumer thread. The messages are (add, 4), (multiply, 1), (multiply, 8), (add, 2), (add, 3), (add, 99), (multiply, 53) sent to the consumer.
QUESTION
I am new to multi-threading, but from what I can tell I have no idea where I'm going wrong. My output is:
Added: add(),4 Added: multiply(),1 Added: multiply(),8 Received Removed: add(),4 Added: add(),2 Consumed: add(),4 Produced: 14
The program is suppose to "consume" and "produce" every element in the array. Here is my source code. Thanks
import java.util.*;
public class Threading{
public static void main(String[] args) {
MessageQueueConnector monitor = new MessageQueueConnector();
Producer pro = new Producer(monitor);
Consumer con = new Consumer(monitor);
pro.start();
con.start();
}
}
class MessageQueueConnector{
//private String[] queue;
private int maxCount;
private int messageCount=0;
private Queue
public MessageQueueConnector() {
this.maxCount=3;
}
public synchronized void send(String message) {
while(messageCount==maxCount) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Added: "+message);
q.add(message);
messageCount++;
if(messageCount==1)
notify();
}
public synchronized String receive() {
System.out.println("Received");
while(messageCount==0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Removed: "+q.peek());
messageCount --;
if(messageCount==maxCount-1)
notify();
return q.remove();
}
}
class Producer extends Thread{
private MessageQueueConnector monitor;
public Producer(MessageQueueConnector monitor) {
this.monitor = monitor;
}
public void run() {
String[] msg = {"add(),4", "multiply(),1", "multiply(),8", "add(),2","add(),3","add(),99", "multiply(),53" };
for(int i=0;i<7;i++) {
//SEND
//System.out.println("Sent: "+msg[i]+" ");
monitor.send(msg[i]);
}
}
}
class Consumer extends Thread{
private MessageQueueConnector monitor;
public Consumer(MessageQueueConnector monitor) {
this.monitor = monitor;
}
public void run() {
//RECEIVE
String msg = monitor.receive();
System.out.println("Consumed: "+msg);
String[] msgArray = msg.split(",");
String op = msgArray[0];
int num = Integer.parseInt(msgArray[1]);
SimpleCalculation cal = new SimpleCalculation(op,num);
cal.check();
}
}
class SimpleCalculation{
private String operator;
private int num;
SimpleCalculation(String operator, int num){
this.operator=operator;
this.num=num;
}
public void check() {
if(operator.startsWith("add()")==true)
add(this.num);
else if(operator.startsWith("multiply()")==true)
multiply(this.num);
}
public void add(int num){
num=num+10;
System.out.println("Produced: "+num);
}
public void multiply(int num) {
num=num*10;
System.out.println("Produced: "+num);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
