Queue full error, working with multiple consumers, producers

Я хотел бы смоделировать следующий сценарий: Несколько потребителей, потоки производителей работают над модификацией некоторых данных как

Setup

    BlockingQueue<String> q1 = new SynchronousQueue<String>();
    BlockingQueue<String> q2 = new SynchronousQueue<String>();

    Producer dataProducer = new Producer(q1); // publish to q1

    Filter1 filter1 = new Filter1(q1, q2);    // read from q1, publish to q2
    Filter2 filter2 = new Filter2(q2);        // read from q2

    new Thread(dataProducer, "Producer-Thread").start();
    new Thread(filter1, "Filter1-Thread").start();
    new Thread(filter2, "Filter2-Thread").start();

Producer

public void run() {
    try {
        while (true) {
            this.q.put(saySomething());
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

public String saySomething() {
    return "Something";
}

Filter 1

public void run() {
    try {
        while (true) {
            consume(qIn.take());
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

private void consume(String take) {
    //Modify data according to some rules
    String newData = take.replace("m", "-");
    produce(newData);
}

private void produce(String newData) {
    // put new data in queue out
    qOut.add(newData);                   // <-- Stacktrace points here
}

Filter 2

public void run() {
    try {
        while (true) {
            consume(qIn.take());
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

private void consume(String s) {
    System.out.println("Something became: " + s);
}

Итак, напомню: Producer помещает что-то в очередь, из которой Filter 1 считывает данные. Он модифицирует данные и публикует их в другой очереди, из которой читает фильтр 2. Фильтр 2 печатает окончательные данные.

Этот код не работает

Exception in thread "Thread-2" java.lang.IllegalStateException: Queue full

Не могли бы вы помочь мне понять, почему?

6
задан James Raitsev 1 February 2012 в 04:02
поделиться