Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生产者-消费者场景,停止从队列读取,直到执行完成_Java_Queue_Producer Consumer_Blockingqueue - Fatal编程技术网

Java 生产者-消费者场景,停止从队列读取,直到执行完成

Java 生产者-消费者场景,停止从队列读取,直到执行完成,java,queue,producer-consumer,blockingqueue,Java,Queue,Producer Consumer,Blockingqueue,我正在使用BlockingQueue实现生产者-消费者场景 这是我的消费者运行方法: @Override public void run() { try { Message msg; System.out.println("Consumer started"); //consuming messages until exit message is received while

我正在使用BlockingQueue实现生产者-消费者场景

这是我的消费者运行方法:

@Override
    public void run() {
        try {
            Message msg;
            System.out.println("Consumer started");
            //consuming messages until exit message is received

            while(!(msg=queue.take()).getMsg().equalsIgnoreCase("exit")) {

                //




            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
当我从队列中得到一些东西时,我打算调用下面的方法,但这里有一个条件。我只想运行此方法的一个实例。在我从下面的方法得到返回之前,我不想从队列中消费

我一直在为如何在while循环中包含这一点而挣扎

public boolean runStrategy(String msg) {

        ExecutionContext executionContext = new ExecutionContext();

        String[] filePathArray = msg.split("/");

        String campaignForType = filePathArray[6];// .equals("Profiles")/events etc etc

        if (campaignForType.equalsIgnoreCase("Profiles")) {

            executionContext.setExecutionStrategy(new ProfileUploadExecutionStrategy());
            return executionContext.executeCampaign(filePathArray, msg);

        } else if (campaignForType.equalsIgnoreCase("Events")) {

           /* executionContext.setExecutionStrategy(new EventExecutionStrategy());
            executionContext.executeCampaign(filePathArray, msg.toString());*/

            return true;
        }

        return true;
    }

一个非常简单的解决方案:只启动一个消费者线程

您最多需要一个正在运行的方法实例。或者还有其他情况吗?。 如果只有一个线程,则只能运行该方法的一个实例。只要该线程忙,就没有其他线程可以从队列中消费