package file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @Package file * @ClassName FileThread.java * @date 2019年4月9日 下午5:14:44 * @version V1.0 */ public class FileThread { public static void main(String[] args) throws Exception { long t1 = System.currentTimeMillis(); // String localFilePath = localTempPath+"/"+fileName; String localFilePath = "D:\a\c\haha.txt"; // 开启固定线程池 ExecutorService exec = Executors.newFixedThreadPool(50); // 逐行读取本地文件 List<String> dataList = new ArrayList<>(); // File f = new File("D:\a\b\in.txt"); File f = new File(localFilePath); InputStreamReader reader = new InputStreamReader(new FileInputStream(f), "GBK"); BufferedReader br = new BufferedReader(reader); String str = null; // 定义计数器 int i = 0; while ((str = br.readLine()) != null) { // i的值是从1开始 i++; // System.out.println(str); // 加入集合 dataList.add(str); if (i % 100 == 0) { System.out.println("i"+i); // 每次传入线程的集合 List<String> onceList = new ArrayList<>(); for (String item : dataList) { onceList.add(item); } // 清空集合 dataList = null; // 重构集合 dataList = new ArrayList<String>(); Map<String, Object> pMap = new HashMap<>(); // 开启线程 Runnable task = new BatchHandlerThreadTask(onceList, pMap); exec.submit(task); } } reader.close(); br.close(); // 判断最后一次 if (dataList.size() != 0) { Map<String, Object> pMap = new HashMap<>(); Runnable task = new BatchHandlerThreadTask(dataList, pMap); exec.submit(task); } exec.shutdown(); while (true) { if (exec.isTerminated()) { System.out.println("全部线程都结束了,i: "+i+"耗时:"+(System.currentTimeMillis()-t1)); break; } } } }
/* * @date 2019年4月4日 上午10:36:03 * @Description: 此处添加文件描述…… */ package file; import java.util.List; import java.util.Map; /** * 批量合规的多线程调用任务 * @Package com.tydic.jtcrm.batch.task * @ClassName BatchHandlerThreadTask.java * @date 2019年4月4日 上午10:36:03 * @version V1.0 */ public class BatchHandlerThreadTask implements Runnable { // protected static Logger logger = LoggerFactory.getLogger(BatchHandlerThreadTask.class); //待处理数据集合 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; } @Override public void run() { long t1 = System.currentTimeMillis(); for (int y = 0; y < dataList.size(); y++) { String s = (String) dataList.get(y); // System.out.println("--t--线程名: " + Thread.currentThread().getName() + "--当前批次处理总数据" + dataList.size() + "--当前数据---" + s); try { Thread.sleep(60); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("--h--线程名: " + Thread.currentThread().getName() + "--当前线程耗时:" + (System.currentTimeMillis() - t1) + "--当前批次处理总数据" + dataList.size()); } }