一个制作人,多个消费者

时间:2015-03-29 14:03:51

标签: java multithreading producer-consumer

我一直在研究一些代码,但我需要帮助。 我创建了一个生产者和一个消费者,但是我需要创建多个消费者,他们将使用生产者的特定String,例如我需要一个消费者,专门消费'左手'。

代码中包含缓冲区,生产者,消费者和主要内容。我不知道如何通知正确的消费者并比较需要消费的字符串。目前我只有一个消费者。

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

2 个答案:

答案 0 :(得分:4)

问题是你的iRobotBuffer类。它需要是一个支持多个生产者/消费者的队列。我已经为这样的队列提供了代码,但java已经有了一个实现(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();
    }
}

答案 1 :(得分:0)

据我了解,您希望有3个消费者,每个移动指令一个。 您可以使用java.util.concurrent包中的ArrayBlockingQueue代替iRobotBuffer类。顺便说一句,你可以看看提供的其他并发集合 - 一个可能会让你更好。

然后,对于消费者,您可以peek()在队列中的内容并测试它是否符合要求,然后poll()