当前位置: 首页 > 知识库问答 >
问题:

为什么我的ArrayBlockingQueue在放入列表后会导致空队列?

孙德宇
2023-03-14

这是我第一次问有关StackOverflow的问题。我的问题如下:

我有一个生产者和消费者类。在Producer类中,我逐行读取文件,并将这些文本行放入字符串列表中。当列表的行数为x时。此列表将添加到ArrayBlockingQueue。我有一个在主线程中启动的生产者线程。除此之外,我还启动了几个消费者线程。使用者线程从队列中获取一个项目,该项目应该是一个列表,并遍历该行列表以查找特定单词。找到单词后,它会增加一个count变量。

结果是,当消费者从队列中取出一个项目时,它会说它是空的。我不明白为什么,因为我的制作人肯定应该把它添加到队列中。

我的代码如下所示:

消费者阶层:

public static class Consumer implements Callable<Integer> {

    int count = 0;

    @Override
    public Integer call() throws Exception {
        List<String> list = new ArrayList<>();
        list = arrayBlockingQueueInput.take();
        do {
            if (!list.isEmpty()){
                for (int i = 0; i < arrayBlockingQueueInput.take().size(); i++) {
                    for (String element : list.get(i).split(" ")) {
                        if (element.equalsIgnoreCase(findWord)) {
                            count++;
                        }
                    }
                }
            } else {
                arrayBlockingQueueInput.put(list);
            }
        } while (list.get(0) != "HALT");
        return count;
    }
}

生产者类别:

public static class Producer implements Runnable {

    @Override
    public void run() {
        try {
            FileReader file = new FileReader("src/testText.txt");
            BufferedReader br = new BufferedReader(file);

            while ((textLine = br.readLine()) != null) {

                if (textLine.isEmpty()) {
                    continue;
                }

                /* Remove punctuation from the text, except of punctuation that is useful for certain words.
                * Examples of these words are don't or re-enter */
                textLine = textLine.replaceAll("[[\\W]&&[^']&&[^-]]", " ");

                /* Replace all double whitespaces with single whitespaces.
                * We will split the text on these whitespaces later */
                textLine = textLine.replaceAll("\\s\\s+", " ");

                textLine = textLine.replaceAll("\\n", "").replaceAll("\\r", "");

                if (results.isEmpty()) {
                    results.add(textLine);
                    continue;
                }
                if (results.size() <= SIZE) {
                    results.add(textLine);
                    if (results.size() == SIZE) {
                        if (arrayBlockingQueueInput.size() == 14){
                            List<String> list = new ArrayList<String>();
                            list.add(HALT);
                            arrayBlockingQueueInput.put(list);
                        } else{
                            arrayBlockingQueueInput.put(results);
                            results.clear();
                        }
                    }
                }
            }
            /* Count the remaining words in the list
             *  (last lines of the file do perhaps not fill up until the given SIZE, therefore need to be counted here)
             *  Fill the list with empty items if the size of the list does not match with the given SIZE */
            while (results.size() != SIZE) {
                results.add("");
            }
            arrayBlockingQueueInput.put(results);
            List<String> list = new ArrayList<String>();
            list.add(HALT);
            arrayBlockingQueueInput.put(list);
            results.clear();
        } catch (InterruptedException e) {
            producerIsRunning = false;
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

主要类别:

public void main() throws IOException, InterruptedException {
    System.out.println("Enter the word you want to find: ");
    Scanner scan = new Scanner(System.in);
    findWord = scan.nextLine();

    System.out.println("Starting...");
    long startTime = System.currentTimeMillis();
    Thread producer = new Thread(new Producer());
    producer.start();
    ExecutorService executorService = Executors.newFixedThreadPool(CORE);

    List<Future<Integer>> futureResults = new ArrayList<Future<Integer>>();

    for (int i = 0; i < CORE; i++) {
        futureResults.add(executorService.submit(new Consumer()));
    }

    executorService.shutdown();

    for (Future<Integer> result : futureResults) {
        try {
            wordsInText += result.get();
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    producer.join();

    long stopTime = System.currentTimeMillis();

    System.out.println("The word " + findWord + " appears " + wordsInText + " times in the given text");

    System.out.println("Elapsed time was " + (stopTime - startTime) + " milliseconds.");
}

有人能解释为什么会这样吗?我想补充一点,我们试图使用毒丸来通知消费者,生产商已经停产。

回答我们为什么要这样做的问题?对于学校来说,我们试图将某个编程问题并行化。我们选择的问题是字符串匹配。我们首先提出了串行解决方案和并行解决方案。在下一个作业中,我们必须改进我们的并行解决方案,老师告诉我们这是一种方法。

提前感谢!

刻痕

共有2个答案

李泓
2023-03-14

在老师的帮助下,他帮助我们找到了问题。有两个错误。其中一个属于制片人阶层。我有代码在主while循环中发出生产者停止的信号。不应该这样做。

除此之外。take()我在Consumer类中执行,而do-While应该在do-While循环中执行。

正确的代码如下所示:

消费者阶层:

public static class Consumer implements Callable<Integer> {

    int count = 0;

    @Override
    public Integer call() throws Exception {
        List<String> list = new ArrayList<>();
        do {
            list = arrayBlockingQueueInput.take();
            if (!list.get(0).equals(HALT)){
                for (int i = 0; i < list.size(); i++) {
                    for (String element : list.get(i).split(" ")) {
                        if (element.equalsIgnoreCase(findWord)) {
                            count++;
                        }
                    }
                }
            } else {
                arrayBlockingQueueInput.put(list);
            }
        } while (!list.get(0).equals(HALT));
        return count;
    }
}

生产者类别:

public static class Producer implements Runnable {

    @Override
    public void run() {
        try {
            FileReader file = new FileReader("src/testText.txt");
            BufferedReader br = new BufferedReader(file);

            while ((textLine = br.readLine()) != null) {

                if (textLine.isEmpty()) {
                    continue;
                }

                /* Remove punctuation from the text, except of punctuation that is useful for certain words.
                * Examples of these words are don't or re-enter */
                textLine = textLine.replaceAll("[[\\W]&&[^']&&[^-]]", " ");

                /* Replace all double whitespaces with single whitespaces.
                * We will split the text on these whitespaces later */
                textLine = textLine.replaceAll("\\s\\s+", " ");

                textLine = textLine.replaceAll("\\n", "").replaceAll("\\r", "");

                if (results.isEmpty()) {
                    results.add(textLine);
                    continue;
                }
                if (results.size() <= SIZE) {
                    results.add(textLine);
                    if (results.size() == SIZE) {
                        arrayBlockingQueueInput.put(new ArrayList<String>(results));
                        results.clear();
                    }
                }
            }
            /* Count the remaining words in the list
             *  (last lines of the file do perhaps not fill up until the given SIZE, therefore need to be counted here)
             *  Fill the list with empty items if the size of the list does not match with the given SIZE */
            while (results.size() != SIZE) {
                results.add("");
            }
            arrayBlockingQueueInput.put(new ArrayList<String>(results));
            List<String> list = new ArrayList<String>();
            list.add(HALT);
            arrayBlockingQueueInput.put(list);
            results.clear();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

主要类:

public void main() throws IOException, InterruptedException {
    System.out.println("Enter the word you want to find: ");
    Scanner scan = new Scanner(System.in);
    findWord = scan.nextLine();

    System.out.println("Starting...");
    long startTime = System.currentTimeMillis();
    Thread producer = new Thread(new Producer());
    producer.start();
    ExecutorService executorService = Executors.newFixedThreadPool(CORE);

    List<Future<Integer>> futureResults = new ArrayList<Future<Integer>>();

    for (int i = 0; i < CORE; i++) {
        futureResults.add(executorService.submit(new Consumer()));
    }

    executorService.shutdown();

    for (Future<Integer> result : futureResults) {
        try {
            wordsInText += result.get();
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    producer.join();

    long stopTime = System.currentTimeMillis();

    System.out.println("The word " + findWord + " appears " + wordsInText + " times in the given text");

    System.out.println("Elapsed time was " + (stopTime - startTime) + " milliseconds.");
}

感谢@Ivan帮了我。清除对结果调用的方法。没有这一点,代码解决方案就无法工作。

景宏盛
2023-03-14

您可以将列表添加到队列并清除它:

arrayBlockingQueueInput.put(results);
results.clear();

您需要这样做才能将列表的副本添加到队列中,以便不会清除队列中的列表:

arrayBlockingQueueInput.put(new ArrayList<String>(results));
results.clear();
 类似资料:
  • 我在C中实现了一个简单的队列,但是当我试图在退出队列后访问Q. ex时,它会出现分段错误(例如,请参阅int main())。 更准确地说,问题发生在我- 将单个元素排队 把它排出来 使一个或多个元素排队 尝试访问Q.front 然而,程序没有给出分段错误或任何错误,当我- 使多个元素排队 这是我的完整程序-

  • 问题内容: 输出: 但是当我执行以下操作时: 输出: 为什么扩展列表引用而不是列表扩展? 我发现这是因为我在尝试将listC扩展到listB时试图将listB附加到listA。 假设列表不能直接附加/扩展。解决该问题最常用的表格形式是什么? 问题答案: 修改到位列表,不返回任何内容,从而导致。在第二种情况下,这是一个正在扩展的临时列表,该列表在该行后立即消失,而在第一种情况下,可以通过引用。 在尝

  • 我对ArrayBlockingQueue进行了一个简单的测试,如下所示: 结果是: 我的问题是,我已经将ArrayBlockingQueue的大小定义为3,而制作人将2、0和1放入队列,总共3个项目,现在队列已满,然后消费者消费了0,队列大小现在应为2,然后制作人将4放入队列,现在队列应已满,为什么制作人仍可以将6放入队列,应该被阻止

  • 这是为编写的: 有界队列。当与有限的maximumPoolSizes一起使用时,有界队列(例如ArrayBlockingQueue)有助于防止资源耗尽,但可能更难调优和控制。队列大小和最大池大小可以相互权衡:使用大队列和小池可以最大限度地减少CPU使用量、OS资源和上下文切换开销,但可能会导致人为的低吞吐量。如果任务经常阻塞(例如,如果它们是I/O绑定的),系统可能能够为更多的线程安排时间,而不是

  • 所以当我在leetcode上运行这个代码时,它告诉我在列表节点中发现错误循环,但是我一生都不知道为什么。请帮忙,谢谢! }

  • 问题内容: 好吧,我试图理解并阅读可能导致它的原因,但我却无法理解: 我的代码中有这个地方: 事实是,当它尝试调用某些方法时,它将引发而不是其他预期的异常(特别是)抛出 。我实际上知道调用了什么方法,所以我直接转到该方法代码,并为应该抛出的行添加了一个块 ,它实际上按预期抛出。然而,当它上升时,以某种方式更改了上面的代码并没有 按预期进行。 是什么原因导致这种行为的?我该如何检查? 问题答案: 通