Search code examples
javamultithreadingproducer-consumer

One Producer, multiple Consumers


I have been working on some code, but I need help. I have created one producer and one consumer, however I need to create multiple consumers who will consume the specific String from the producer e.g. I need a consumer that will consume specifically 'Move Left Hand'.

Contained in the code is the buffer, producer, consumer and the main. I am not sure how to notify the correct consumer and compare the string that needs to be consumed. As it stands I only have one consumer.

public class iRobotBuffer {
    private boolean empty = true;

    public synchronized String take() {
        // Wait until message is
        // available.
        while (empty) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        // Toggle status.
        empty = true;
        // Notify producer that
        // status has changed.
        notifyAll();
        return message;
    }

    public synchronized void put(String message) {
        // Wait until message has
        // been retrieved.
        while (!empty) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        // Toggle status.
        empty = false;
        // Store message.
        this.message = message;
        // Notify consumer that status
        // has changed.
        notifyAll();
    }
}

public class iRobotConsumer implements Runnable {
    private iRobotBuffer robotBuffer;

    public iRobotConsumer(iRobotBuffer robotBuffer){
        this.robotBuffer = robotBuffer;
    }

    public void run() {
        Random random = new Random();
        for (String message = robotBuffer.take();
                ! message.equals("DONE");
                message = robotBuffer.take()) {
            System.out.format("MESSAGE RECEIVED: %s%n", message);
            try {
                Thread.sleep(random.nextInt(5000));
            } catch (InterruptedException e) {}
        }
    }
}

public class iRobotProducer implements Runnable {
    private iRobotBuffer robotBuffer;
    private int number;

    public iRobotProducer(iRobotBuffer robotBuffer)
    {  
        this.robotBuffer = robotBuffer;
        //this.number = number;
    }

    public void run() {
        String commandInstructions[] = {
                "Move Left Hand",
                "Move Right Hand",
                "Move Both Hands",
        };
        int no = commandInstructions.length;
        int randomNo;
        Random random = new Random();


        for (int i = 0;
                i < commandInstructions.length;
                i++) {
            randomNo =(int)(Math.random()*no);
            System.out.println(commandInstructions[randomNo]);

            robotBuffer.put(commandInstructions[i]);
            try {
                Thread.sleep(random.nextInt(5000));
            } catch (InterruptedException e) {}
        }
        robotBuffer.put("DONE");
    }
}

public class iRobot
{
    public static void main(String[] args)
    {
        iRobotBuffer robotBuffer = new iRobotBuffer();
        (new Thread(new iRobotProducer(robotBuffer))).start();
        (new Thread(new iRobotConsumer(robotBuffer))).start();

    }//main
}//class

Solution

  • The problem is your iRobotBuffer class. It needs to be a queue to support multiple producer / consumers. I've provided the code for such a queue, but java already has an implementation (BlockingDeque<E>).

    public class BlockingQueue<T> {
    
        private final LinkedList<T> innerList = new LinkedList<>();
        private boolean isEmpty = true;
    
        public synchronized T take() throws InterruptedException {
            while (isEmpty) {
                wait();
            }
    
            T element = innerList.removeFirst();
            isEmpty = innerList.size() == 0;
            return element;
        }
    
        public synchronized void put(T element) {
            isEmpty = false;
            innerList.addLast(element);
            notify();
        }
    }