//主函数
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
// String localFilePath = localTempPath+"/"+fileName;
String localFilePath = "读取文件地址";
ReadFile(startTime, localFilePath);
}
//读文件
private static void ReadFile(long startTime, String localFilePath) throws IOException {
// 开启固定线程池
//每个机器测试时间不同仅供参考
//9s 定长线程池
//ExecutorService exec = Executors.newFixedThreadPool(48);
//9s 缓存线程池
//ExecutorService exec = Executors.newCachedThreadPool();
//11s 周期线程池
//ExecutorService exec = Executors.newScheduledThreadPool(48);
//18S 单例线程池
ExecutorService exec = Executors.newSingleThreadExecutor();
List dataList = new ArrayList<>();
// 逐行读取本地文件
File f = new File(localFilePath);
//读取文件
InputStreamReader reader = new InputStreamReader(new FileInputStream(f), "UTF-8");
BufferedReader br = new BufferedReader(reader);
String str = null;
// 定义计数器
int i = 0;
//判断空行
while ((str = br.readLine()) != null) {
// i的值是从1开始
i++;
// 加入集合
dataList.add(str);
if (i % 100 == 0) {
System.out.println("成百计时:"+i);
// 每次传入线程的集合
List onceList = new ArrayList<>();
for (String item : dataList) {
onceList.add(item);
}
// 清空集合
dataList = null;
// 重构集合
dataList = new ArrayList();
Map pMap = new HashMap<>();
// 开启线程
Runnable task = new BatchHandlerThreadTask(onceList, pMap);
exec.submit(task);
}
}
reader.close();
br.close();
// 判断最后一次
if (dataList.size() != 0) {
Map pMap = new HashMap<>();
Runnable task = new BatchHandlerThreadTask(dataList, pMap);
exec.submit(task);
}
exec.shutdown();
/*
*
isShutDown当调用shutdown()或shutdownNow()方法后返回为true。
isTerminated当调用shutdown()方法后,并且所有提交的任务完成后返回为true;
isTerminated当调用shutdownNow()方法后,成功停止后返回为true;
*/
while (true) {
if (exec.isTerminated()) {
System.out.println("全部线程都结束了,i: "+i+"----耗时:"+(System.currentTimeMillis()-startTime)/1000+"s");
break;
}
}
}
import java.io.*;
import java.util.List;
import java.util.Map;
/**
* 实现Runnable接口
* @Auther Qianxy
* @Date 2020/7/3
*/
public class BatchHandlerThreadTask implements Runnable {
//待处理数据集合
private List dataList;
//其他参数Map
private Map paramMap;
public BatchHandlerThreadTask() {
super();
}
public BatchHandlerThreadTask(List dataList, Map paramMap) {
super();
this.dataList = dataList;
this.paramMap = paramMap;
}
public List getDataList() {
return dataList;
}
public void setDataList(List dataList) {
this.dataList = dataList;
}
public Map getParamMap() {
return paramMap;
}
public void setParamMap(Map paramMap) {
this.paramMap = paramMap;
}
//写文件
public void writeListToFile(List dataList) {
// 要写入的文件路径
File file = new File("写入文件地址");
// 判断文件是否存在
if (!file.exists()) {
try {
// 如果文件不存在创建文件
file.createNewFile();
System.out.println("文件"+file.getName()+"不存在已为您创建!");
} catch (IOException e) {
System.out.println("创建文件异常!");
e.printStackTrace();
}
} else {
System.out.println("文件"+file.getName()+"已存在!");
}
FileOutputStream fos = null;
PrintStream ps = null;
// 遍历listStr集合
for (String str : dataList) {
try {
// 文件输出流
fos = new FileOutputStream(file,true);//追加
ps = new PrintStream(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// +换行
String string = str + "\r\n";
// 执行写操作
ps.print(string);
}
// 关闭流
ps.close();
System.out.println("文件写入完毕!");
}
@Override
public void run() {
//记时间
long t1 = System.currentTimeMillis();
//写入文件
writeListToFile(dataList);
System.out.println("--h--线程名: " + Thread.currentThread().getName() + "--当前线程耗时:" + (System.currentTimeMillis() - t1)/1000 + "s" + "--当前批次处理总数据" + dataList.size());
}
}