从我的队列中取出字符串,把它们加在一起,这样就可以打印出结果



我有一个项目,我必须做一个WordCounter..我已经做了,它工作得很好。

然后我需要创建一个队列,这样我就可以获得不同长度的文本并将它们加在一起并打印出来。

我制作了一个队列,并试图将其打印出来,它也打印出文本中单词的数量,并可以看到它将数字添加到队列中..

但是我不知道如何把这些数字从队列中取出来,这样我就可以把它们加起来了

这是我的WordCounterTester

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class WordCountTest {
public static void main(String[] args){
    final BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
    for(int i = 0; i< args.length; i++){
        Runnable r = new WordCount(args[i],queue);
        Thread t = new Thread(r);
        t.start();
    }
}
}  

和我的WordCounter

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
class WordCount implements Runnable {
public int result;
private String s;
Thread runner;
private BlockingQueue<String> queue;
public WordCount(String s, BlockingQueue<String> queue){
    this.s = s;
    this.queue = queue;
}
public void run() {
    try{
        FileReader fr = new FileReader(s);
        Scanner scanner = new Scanner(fr);
        for(int i = 0; true; i++){
            result = i;
            scanner.next();
        }
    }catch(FileNotFoundException e){
        System.out.println("Du har skrevet en fil der ikke findes, den fil vil blive skrevet ud med 0 ord");
    }
    catch(NoSuchElementException e){}
    System.out.println(s + ": " + result);
    Integer.toString(result);
    queue.add(result + "");
    System.out.println(queue);
}
}

我现在得到的是当我用6个不同的。txt运行程序时,顺序可以不同,因为它是多线程的,所以哪个先完成是不同的:)

5.txt: 1
[1]
6.txt: 90
[1, 90]
4.txt: 576
[1, 90, 576]
3.txt: 7462
[1, 90, 576, 7462]
1.txt: 7085
[1, 90, 576, 7462, 7085]
2.txt: 11489
[1, 90, 576, 7462, 7085, 11489]

有谁知道我该怎么做吗?

我认为你缺少的是main需要加入它在每个线程将其每个文件计数添加到队列后产生的线程。

public static void main(String[] args){
    final BlockingQueue<Integer> queue = new LinkedBlockingQueue<String>();
    Thread[] threads = new Thread[args.length];
    for (int i = 0; i< args.length; i++){
        Runnable r = new WordCount(args[i], queue);
        threads[i] = new Thread(r);
        threads[i].start();
    }
    // wait for the threads to finish
    for (Thread t : threads) {
        t.join();
    }
    int total = 0;    
    for (Integer value : queue) {
        total += value;
    }
    System.out.println(total);
}

在这里的代码中,我实现了@Ben van Gompel的建议,将Integer添加到队列中,而不是String

您也可以使用AtomicInteger类并执行如下操作:

final AtomicInteger total = new AtomicInteger(0);
...
    Runnable r = new WordCount(args[i], total);
...
// join loop
// no need to total it up since each thread would be adding to the total directly
System.out.println(total);

在WordCount代码内,您可以使用AtomicInteger,如:

System.out.println(s + ": " + result);
// add the per-file count to the total
total.incrementAndGet(result);
// end

首先,我将队列的类型更改为BlockingQueue<Integer>,因为在这里使用String似乎没有任何意义。在你的线程完成之后(你为什么要使用线程呢?)你应该迭代queue中的值并把它们加起来。

int total = 0;    
for (Integer value : queue) {
  total += value;
}
System.out.println(total);

相关内容

最新更新