如何从Swing或控制台访问BlockingQueue?

时间:2014-07-10 16:20:14

标签: java multithreading design-patterns producer-consumer blockingqueue

无论是来自GUI还是控制台,如何与Consumer互动?我应该简单地将consume公开,还是打包私有?这似乎是它进入一个雷区。

消费者:

package net.bounceme.dur.client;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.bounceme.dur.data.State;
import net.bounceme.dur.data.Title;

public class Consumer implements Runnable {

    private final BlockingQueue<Title> queue;
    private ObjectOutputStream objectOutputStream = null;

    public Consumer(BlockingQueue<Title> queue, ObjectOutputStream objectOutputStream) {
        this.queue = queue;
        this.objectOutputStream = objectOutputStream;
    }

    private void consume() throws InterruptedException, IOException {
        Title title = queue.take();
        title.setState(State.x);
        objectOutputStream.writeObject(title);
    }

    @Override
    public void run() {
        try {
            consume();
        } catch (InterruptedException | IOException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
            //exit?
        }
    }
}

服务:

package net.bounceme.dur.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.bounceme.dur.data.Title;

public class ProducerConsumerService implements Executor {

    private static final Logger log = Logger.getLogger(ProducerConsumerService.class.getName());
    private final BlockingQueue<Title> queue = new ArrayBlockingQueue<>(1);
    private Producer producer = null;
    private Consumer consumer = null;

    public void ProducerConsumerService() {
    }

    public void produce() {
    }

    public void consume() {
    }

    public void startService() throws IOException {
        Properties props = PropertiesReader.getProps();
        int portNumber = Integer.parseInt(props.getProperty("port"));
        String host = props.getProperty("server");
        Socket socket = new Socket(host, portNumber);
        ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        producer = new Producer(queue, objectInputStream);
        consumer = new Consumer(queue, objectOutputStream);
        new Thread((Runnable) producer).start();
        new Thread((Runnable) consumer).start();
        log.info("started...");
    }

    @Override
    public void execute(Runnable command) {
        new Thread(command).start();
    }
}

或者,或者,我是否在服务中添加方法来访问BlockingQueue?但是,这样做似乎不需要Consumer类本身。

外部类(Console或Swing接口)对这三个类的正确用法是什么?

参考:

http://www.journaldev.com/1034/java-blockingqueue-example-implementing-producer-consumer-problem

1 个答案:

答案 0 :(得分:0)

我对以下解决方案的关注:

package net.bounceme.dur.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import net.bounceme.dur.data.Title;

public class ProducerConsumerService implements Executor {

    private static final Logger log = Logger.getLogger(ProducerConsumerService.class.getName());
    private final BlockingQueue<Title> queue = new ArrayBlockingQueue<>(1);
    private Producer producer = null;
    private Consumer consumer = null;
    private Title title = null;

    public void ProducerConsumerService() {
    }

    public Title produce() throws InterruptedException, IOException, ClassNotFoundException {
        producer.produce();
        title = queue.take();
        consumer.consume(title);
        return title;
    }

    public void startService() throws IOException {
        Properties props = PropertiesReader.getProps();
        int portNumber = Integer.parseInt(props.getProperty("port"));
        String host = props.getProperty("server");
        Socket socket = new Socket(host, portNumber);
        ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        producer = new Producer(queue, objectInputStream);
        consumer = new Consumer(queue, objectOutputStream);
        new Thread((Runnable) producer).start();
        new Thread((Runnable) consumer).start();
        log.info("started...");
    }

    @Override
    public void execute(Runnable command) {
        new Thread(command).start();
    }
}

是通过在服务中通过Produceer方法暴露过多的Consumerproduce()来打破封装。