简单的线程池实现多线程对大文件的读取

上代码
package ThreadLearning;
【简单的线程池实现多线程对大文件的读取】import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**

  • @author kyojurorengoku
  • @date 2022/1/20
  • @Description
    */
public class LstFileAnalysis {
static String fileUrl = ""; static File file = new File(fileUrl); static InputStreamReader isr; static { try { isr = new InputStreamReader(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } }public static void main(String[] args) throws IOException { ExecutorService executorService = Executors.newFixedThreadPool(4); BufferedReader read = new BufferedReader(isr); String line = ""; long l = System.currentTimeMillis(); while ((line = read.readLine()) != null) { String[] split = line.split("\s+"); if (split.length == 0) { break; } Runnable task = new Task(line); executorService.execute(task); } long l1 = System.currentTimeMillis(); System.out.println((l1 - l)); }

}
复制代码
Task类 提供一个简单的思路: 1、读取出来的数据处理过后存入数据库[写在run方法里就行]
package ThreadLearning;
import java.util.List;
/**
  • @author kyojurorengoku
  • @date 2022/1/20
  • @Description
    */
public class Task implements Runnable {
private String line; public Task(String line) { this.line = line; }@Override public void run() {

// System.err.println(Thread.currentThread().getName() + " " + line);
}

}
最后
如果你觉得此文对你有一丁点帮助,点个赞。或者可以加入我的开发交流群:1025263163相互学习,我们会有专业的技术答疑解惑
如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点star:http://github.crmeb.net/u/defu不胜感激 !
PHP学习手册:https://doc.crmeb.com
技术交流论坛:https://q.crmeb.com

    推荐阅读