我正在尝试读取一个文件并将每一行添加到一个列表中。
Simple drawing explaining the goal
主类-
public class SimpleTreadPoolMain {
public static void main(String[] args) {
ReadFile reader = new ReadFile();
File file = new File("C:\\myFile.csv");
try {
reader.readFile(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}Reader类-
public class ReadFile {
ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads
List<String> list = new ArrayList<>();
void readFile(File file) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != "") {
Runnable saver = new SaveToList(line,list);
executor.execute(saver);//calling execute method of ExecutorService
}
}
executor.shutdown();
while (!executor.isTerminated()) { }
}
}Saver类-
public class SaveToList<E> implements Runnable{
List<E> myList;
E line;
public SaveToList(E line, List<E> list) {
this.line = line;
this.myList = list;
}
public void run() {
//modify the line
myList.add(line);
}
}我尝试让多个保存线程添加到同一个列表中,而不是一个保存线程逐个添加到列表中。我想使用线程,因为我需要在添加到列表之前修改数据。因此,我假设修改数据会占用一些时间。因此,并行这一部分将减少时间消耗,对吧?
但这不管用。我无法返回包含文件中所有值的全局列表。我希望文件中只有一个全局值列表。因此,代码肯定应该更改。如果有人能指导我,我将不胜感激。
尽管在一个线程中一个接一个地添加就可以了,但是使用线程池会更快,对吧?
发布于 2017-04-28 20:27:59
在这里,使用多线程不会加速任何事情。
你是:
从文件中读取一行,serially.
中
假设您正在使用一个ArrayList,那么您需要同步对它的访问,因为您正在从多个线程改变它。所以,您是在连续地向列表中添加内容。
但是,即使没有同步,IO所用的时间也将远远超过将字符串添加到列表中所用的时间。而添加多线程只会让它变得更慢,因为它要做的工作是构造runnable,提交它到线程池,调度它,等等。
更简单的做法是忽略整个中间步骤:
serially.
所以:
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while (!(line = br.readLine()).isEmpty()) {
list.add(line);
}
}https://stackoverflow.com/questions/43680085
复制相似问题