我试图分别计算三个文本文件中的单词数。三个类在多线程中运行。在每个线程中,计算出的单词的总和将被传递给类计数器,并在所有线程完成后更新为计数器的所有计数的总和。
我遇到两个问题,即“setWordCount”可以由类设置器更新。但它不能通过同一类中的吸气剂获得。其他问题是我无法检查Readfiles线程是否存活。
我在网上搜索过& amp;拍多次却无法解决,欢迎任何评论,非常感谢!
NumberWords类: 公共类NumberWords { private int activeThread; private int totalWordCount; private String getThreadName;
public NumberWords(int activeThread) {
this.activeThread = activeThread;
}
public synchronized void incTotalWordCount(int n) {
totalWordCount += n;
System.out.println("The total word count is " + totalWordCount);
}
public synchronized void printCount() {
System.out.println("The total word count(print count) is " + totalWordCount);
}
public synchronized void decActiveThread() {
getThreadName = Thread.currentThread().getName();
System.out.println(getThreadName);
if (Thread.getAllStackTraces().keySet().size() != 0) {
// Get number of active threads
activeThread = Thread.getAllStackTraces().keySet().size();
activeThread--;
if (activeThread == 0) {
System.out.println("The total number of word in the three files is " + totalWordCount);
System.out.println("The active threads is/are " + activeThread);
}
}
}
}
读取文件并计算单词数量:
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
public class ReadFile1 extends Thread implements Runnable {
private NumberWords nw;
public ReadFile1(NumberWords nw) {
this.nw = nw;
}
//set file path
String path1 = System.getProperty("user.dir") + File.separator + "text1.txt";
BufferedReader br1 = null;
public void run() {
try {
br1 = new BufferedReader(new FileReader(path1));
String contentLine = br1.readLine();
while (contentLine != null) {
// Count number of words
String[] parts = contentLine.split(" ");
int count = 0;
for (int i = 0; i < parts.length; i++) {
count++;
}
// Check number of words counted.
System.out.println("The number of words in file(text1.txt) is " + count);
//Pass words count to sum
nw.incTotalWordCount(count);
contentLine = br1.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br1 != null) {
br1.close();
}
} catch (IOException ioe) {
System.out.println("Error in closing the BufferedReader");
}
}
}
}
主要课程:
public class TestReadFile {
public static void main(String args[]){
NumberWords nw = new NumberWords(args.length);
Thread rf1 = new Thread(new ReadFile1(nw));
Thread rf2 = new Thread(new ReadFile2(nw));
Thread rf3 = new Thread(new ReadFile3(nw));
rf1.start();
rf2.start();
rf3.start();
//Get total word cont
nw.decActiveThread();
}
}
答案 0 :(得分:0)
这个怎么样:
public class NumberWords {
private int totalWordCount;
private int activeThreads;
public synchronized void incTotalWordCount(int n) {
totalWordCount += n;
activeThreads--;
System.out.println("The total word count is " + totalWordCount);
}
public synchronized void printCount() {
System.out.println("The total word count(print count) is " + totalWordCount);
}
public int getActiveThreads() {
return activeThreads;
}
public void setActiveThreads(int activeThreads) {
this.activeThreads = activeThreads;
}
}
ReadFile的:
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ReadFile implements Runnable {
private NumberWords nw;
private Path filePath;
public ReadFile(NumberWords nw, Path filePath) {
this.nw = nw;
this.filePath = filePath;
}
BufferedReader br1 = null;
public void run() {
try {
int count = 0;
for (String line : Files.readAllLines(filePath)) {
String[] parts = line.split(" ");
for (String w : parts) {
count++;
}
}
nw.incTotalWordCount(count);
} catch (IOException e) {
e.printStackTrace();
}
}
}
主:
import java.io.File;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String args[]) throws InterruptedException {
NumberWords nw = new NumberWords();
ExecutorService execSvc = Executors.newCachedThreadPool();
new Thread(new ReadFile(nw, Paths.get(System.getProperty("user.dir") + File.separator + "text1.txt"))).start();
new Thread(new ReadFile(nw, Paths.get(System.getProperty("user.dir") + File.separator + "text2.txt"))).start();
new Thread(new ReadFile(nw, Paths.get(System.getProperty("user.dir") + File.separator + "text3.txt"))).start();
nw.setActiveThreads(3);
while (nw.getActiveThreads() > 0) {
Thread.sleep(100);
System.out.println("Waiting for Tasks to complete!");
}
nw.printCount();
}
}