乐趣区

关于php:简单的线程池实现多线程对大文件的读取

上代码

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

退出移动版