首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

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
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43680085

复制
相关文章

相似问题

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