[英]Why does my ArrayBlockingQueue result in an empty queue after putting a list in it?
这是我第一次在StackOverflow上提出问题。 我遇到的问题如下:
我有一个制作人和消费者类。 在Producer类中,我逐行读取文件,并将这些文本行放在字符串列表中。 当列表具有x行数时。 此列表将添加到ArrayBlockingQueue。 我有一个在主线程中启动的生产者线程。 除此之外,我开始使用几个Consumer线程。 Consumer线程从队列中获取一个项目,该项目应该是一个列表,并遍历此行列表以查找特定单词。 当找到该单词时,它会递增计数变量。
发生的事情是当Consumer从队列中获取一个项目时,它表示它是空的。 我无法弄清楚为什么,因为我的制作人当然应该将它添加到队列中。
我的代码看起来像这样:
消费者阶层:
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.");
}
任何人都可以解释为什么会这样吗? 我想补充一点,我们尝试使用毒丸来通知消费者生产者是在HALT上。
要回答这个问题我们为什么要这样做呢? 对于学校,我们尝试并行化某个编程问题。 我们选择的问题是字符串匹配。 我们首先制作了串行解决方案和并行解决方案。 对于下一个任务,我们必须改进我们的并行解决方案,我们的老师告诉我们这是一种方法。
提前致谢!
缺口
您将列表添加到队列并清除它:
arrayBlockingQueueInput.put(results);
results.clear();
您需要执行以下操作以将列表副本添加到队列中,以便clear()
不会清除队列中的列表:
arrayBlockingQueueInput.put(new ArrayList<String>(results));
results.clear();
在老师的帮助下,他帮我们找到了问题。 有两个错误。 一个是生产者类。 我有代码在主while循环中发出生产者的HALT信号。 这不应该做。
除此之外,我在Do-While之前在Consumer类中执行的.take()应该在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帮助我使用.clear方法调用结果。 没有这个,代码解决方案就不起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.