簡體   English   中英

從我的隊列中取出數字即String並將其加在一起,以便可以打印出結果

[英]Take String that is numbers from my queue out and add them together so can print out the result

我有一個必須在其中創建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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM