首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用线程池添加到列表中

使用线程池添加到列表中
EN

Stack Overflow用户
提问于 2017-04-28 20:17:51
回答 2查看 1.2K关注 0票数 0

我正在尝试读取一个文件并将每一行添加到一个列表中。

Simple drawing explaining the goal

主类-

代码语言:javascript
复制
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类-

代码语言:javascript
复制
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类-

代码语言:javascript
复制
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);

}
}

我尝试让多个保存线程添加到同一个列表中,而不是一个保存线程逐个添加到列表中。我想使用线程,因为我需要在添加到列表之前修改数据。因此,我假设修改数据会占用一些时间。因此,并行这一部分将减少时间消耗,对吧?

但这不管用。我无法返回包含文件中所有值的全局列表。我希望文件中只有一个全局值列表。因此,代码肯定应该更改。如果有人能指导我,我将不胜感激。

尽管在一个线程中一个接一个地添加就可以了,但是使用线程池会更快,对吧?

EN

回答 2

Stack Overflow用户

发布于 2017-04-28 20:27:59

在这里,使用多线程不会加速任何事情。

你是:

从文件中读取一行,serially.

  • Creating a runnable并将其提交到线程池中然后将内容添加到list

假设您正在使用一个ArrayList,那么您需要同步对它的访问,因为您正在从多个线程改变它。所以,您是在连续地向列表中添加内容。

但是,即使没有同步,IO所用的时间也将远远超过将字符串添加到列表中所用的时间。而添加多线程只会让它变得更慢,因为它要做的工作是构造runnable,提交它到线程池,调度它,等等。

更简单的做法是忽略整个中间步骤:

serially.

  • Add
  • 从文件中读取一行,并将列表按顺序添加到列表中。

所以:

代码语言:javascript
复制
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while (!(line = br.readLine()).isEmpty()) {
        list.add(line);
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-04-28 21:25:25

事实上,你应该尝试一下是否值得在你的应用程序中使用多线程,只需比较读取整个文件而不对行进行任何处理所需的时间,并将其与串行处理整个文件所需的时间进行比较。

如果你的过程不是太复杂,我的猜测是不值得使用多线程。

如果你发现它花费的时间要长得多,那么你可以考虑使用一个或多个线程来做计算。

如果是这样的话,您可以使用Future处理成批的输入字符串,或者您可以使用线程安全队列将字符串发送到另一个进程。

代码语言:javascript
复制
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读取具有足够大缓冲区的文件可以比并行计算更多地减少时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43680085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档