我正在尝试读取文件并将每行添加到列表中。
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();
}
}
}
读者课程 -
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);
}
}
我尝试将多个保护程序线程添加到同一个列表中,而不是逐个添加到列表中的一个保护程序。我想使用线程,因为我需要在添加到列表之前修改数据。所以我认为修改数据需要一些时间。所以平行这部分可以减少时间消耗,对吗?
但这不起作用。我无法返回包含文件中所有值的全局列表。我想从文件中只有一个全局值列表。所以代码肯定应该改变。如果有人能指导我,我将不胜感激。
即使在单个线程中逐个添加也行,但使用线程池会使它更快,对吧?
答案 0 :(得分:3)
使用多个线程不会在这里加快速度。
你是:
鉴于您正在使用ArrayList
,您需要同步对其的访问权限,因为您要从多个线程中对其进行变更。所以,你是按顺序在列表中添加内容。
但即使没有同步,IO所花费的时间也会远远超过将字符串添加到列表中所花费的时间。添加多线程只会减慢它的速度,因为它正在构建runnable,将它提交给线程池,安排它等等。
错过整个中间步骤更简单:
所以:
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while (!(line = br.readLine()).isEmpty()) {
list.add(line);
}
}
答案 1 :(得分:0)
实际上你应该尝试在你的应用程序中使用多线程是否值得,只需比较读取整个文件所花费的时间而不对任何行进行任何处理,并将其与所需的时间进行比较串行处理整个文件。
如果你的过程不是太复杂,我猜是不值得使用多线程。
如果您发现花费的时间多得多,那么您可以考虑使用一个或多个线程来进行计算。
如果是这样,您可以使用Future
来处理批量输入字符串,或者您可以使用线程安全队列将字符串发送到另一个进程。
private static final int BATCH_SIZE = 1000;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("big_file.csv"), "utf-8"));
ExecutorService pool = Executors.newFixedThreadPool(8);
String line;
List<String> batch = new ArrayList<>(BATCH_SIZE);
List<Future> results = new LinkedList<>();
while((line=reader.readLine())!=null){
batch.add(line);
if(batch.size()>=BATCH_SIZE){
Future<Object> f = noWaitExec(batch, pool);
results.add(f);
batch = new ArrayList<>(BATCH_SIZE);
}
}
Future<List> f = noWaitExec(batch,pool);
results.add(f);
for (Future future : results) {
try {
Object object = future.get();
// Use your results here
} catch (Exception e) {
// Manage this....
}
}
}
private static Future<List> noWaitExec(final List<String> batch, ExecutorService pool) {
return pool.submit(new Callable<List>() {
public List call() throws Exception {
List result = new ArrayList<>(batch.size());
for (String string : batch) {
result.add(process(string));
}
return result;
}
});
}
private static Object process(String string) {
// Your process ....
return null;
};
还有许多其他可能的解决方案(Observables,ParallelStreams,Pipes,CompletableFutures ......你可以命名),我仍然认为大部分时间都是读取文件所需的时间,只需使用BufferedInputStream来读取具有足够大缓冲区的文件可以比平行计算更节省时间。