[英]Multithread file reading
我試圖用Java編寫代碼以通過幾個線程讀取文件並計算其中的單詞。 每個線程應讀取不同的行。 它可以很好地計數單詞(當我讓1個線程運行時),但是我的線程正在讀取同一行並同時增加行計數器。 我確定read方法中的synchronized
關鍵字會解決它,但是沒有。 我該怎么解決?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class WordCounterr implements Runnable {
private static Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
private int lineCounter;
private String path;
private int tNumber;
//private final AtomicInteger whichLine = new AtomicInteger();
private static int whichLine;
private static boolean flag;
public WordCounterr(String path,int num){
lineCounter = 0;
//whichLine = 0;
flag= false;
this.path=path;
tNumber = num;
}
public void countWords(String s) throws IOException{
char[] c = s.toCharArray();
String str="";
char ch;
for(int k=0;k<c.length;k++){
ch=c[k];
if((ch>40 && ch<91) ||(ch>96 && ch<123)){
if(ch>40 && ch<91)
ch+=32;
str+=ch;
}
else if(ch==32 ||k==c.length-1){
if(str.length()>1){ //sprawdzamy czy funkcja znalazla juz
if(ht.containsKey(str)) //takie slowo
ht.put(str,ht.get(str)+1); //znalazla - powiekszamy wartosc przy kluczu
else
ht.put(str,1); //nie znalazla - dodajemy slowo do Hashtable
}
str="";
}
}
}
public synchronized void read(String path) throws IOException{
BufferedReader buf=new BufferedReader(new FileReader(path));
String linia ;
for(int i=0;i<whichLine;i++){
linia=buf.readLine();
}
if((linia=buf.readLine())!=null){
System.out.println(linia);
countWords(linia);
lineCounter++;
System.out.println("watek nr:"+tNumber+"ktora linia:"+whichLine);
whichLine++;
/*try{
Thread.sleep(100);
}catch(InterruptedException el){
System.out.println(el.toString());
}*/
} else
setFlag(true);
buf.close(); //pamietamy o zamknieciu pliku
}
public synchronized void print(){
if(getFlag()){
setFlag(false);
System.out.println(ht);
}
System.out.println("watek nr: "+tNumber+", przeanalizowano "+ lineCounter+ "linii tekstu");
}
public void setFlag(boolean val){
flag=val;
}
public boolean getFlag(){
return flag;
}
@Override
public void run() {
try{
while(getFlag()==false) {
read(path);
Thread.yield(); //let other thread read
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}catch(IOException ex){
System.out.println(ex.toString());
}//catch(InterruptedException el){
// System.out.println(el.toString());
//}
print();
}
public static void main(String[] args) throws IOException, InterruptedException{
String path = args[0];
int tNum = Integer.parseInt(args[1]);
Thread[] thread = new Thread[tNum]; // tablica w?tków
for (int i = 0; i < tNum; i++){
thread[i] =new Thread(new WordCounterr(path,i));
}
for (int i = 0; i < tNum; i++)
thread[i].start();
}
}
定義了synced修飾符是這樣的: it is not possible for two invocations of synchronized methods on the same object to interleave.
您正在調用在每個Threads
read
的方法。
但是,您沒有調用相同的 read
方法,因為您是將WordCounterr
新實例傳遞給每個新Thread
。 這意味着您正在不同的對象上調用該方法,而該方法不會受到synced修飾符的影響。
要解決此問題,請嘗試:
WordCounterr reader = new WordCounterr(path,0); //I changed i to 0 because it can't differentiate between threads with a simple int. This is because each Thread now references the same object.
Thread[] thread = new Thread[tNum]; // tablica w?tków
for (int i = 0; i < tNum; i++){
thread[i] =new Thread(reader);
}
而不是:
Thread[] thread = new Thread[tNum]; // tablica w?tków
for (int i = 0; i < tNum; i++){
thread[i] =new Thread(new WordCounterr(path,i));
}
我希望這有幫助 :)
我猜它仍然會低效地讀取文件內容。 嘗試更改同步點。 應該將其放置在讀取方法中。 此方法讀取整個文件內容。 而是嘗試同步,僅讀取此文件的下一行。 您可以通過為每個WordCounterr實例放置相同的閱讀器文件實例並僅同步將指針移到下一行的過程來讀取該行的內容來實現此目的。 行中的單詞計數無需同步即可完成,僅更新HashTable應該同步。 可以並行同步讀取文件內容,如下所示:
static class Reader implements Runnable {
int lineReaded = 0;
final Scanner scanner;
Reader(Scanner scanner) {
this.scanner = scanner;
}
public void run() {
boolean hasNext = true;
while (hasNext) {
hasNext = false;
synchronized (scanner) {
if (scanner.hasNext()) {
hasNext = true;
String line = scanner.nextLine();
++lineReaded;
}
}
try {
Thread.sleep((long) (Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.