1. 程式人生 > >多執行緒讀取日誌檔案

多執行緒讀取日誌檔案

import com.cndatacom.service.ServiceOrderLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 基於jdk 提供的多執行緒屏障技術 * Created by Administrator on 2018/3/19. */ @Service("ThreadReadLogTask") public class
ThreadReadLogsTask implements Runnable{ //建立初始化N個執行緒的執行緒池 private ExecutorService threadPool; //建立NCyclicBarrier物件,執行完後執行當前類的run方法 private CyclicBarrier cb; //儲存讀取結果--併發程式設計中常用的分段鎖map private ConcurrentHashMap<String, String> map=new ConcurrentHashMap<String,String>(); @Autowired private
ServiceOrderLogService serviceOrderLogService; //第一步載入 public void readLogs(){ map = new ConcurrentHashMap<String,String>(); String path = null; try{ path="d:/service/logfile"; firstReadFile(path); }catch (Exception e){ } } //遞迴遍歷某資料夾下的所有檔案 private void firstReadFile(String filepath)throws IOException { File file = new File(filepath); String[] filelist = file.list(); //建立初始化N個執行緒的執行緒池 threadPool=Executors.newFixedThreadPool(filelist.length); //建立NCyclicBarrier物件,執行完後執行當前類的run方法 cb=new CyclicBarrier(filelist.length,this); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); System.out.println("檔案目錄:"+readfile.getPath()); if (readfile.isDirectory()) {//目錄 threadRead t = new threadRead(); t.setFilePath(readfile.getPath()); //此步驟後呼叫threadReadrun方法 threadPool.execute(t); } } } class threadRead implements Runnable{ private Logger logger = LoggerFactory.getLogger(threadRead.class); private String filePath; public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } @Override public void run() { try { String tempPath = filePath; System.out.println("開始讀取檔案:"+filePath); readfile(tempPath); map.put(Thread.currentThread().getName(), "檔案:"+tempPath+" 讀取完畢"); } catch (IOException e) { e.printStackTrace(); } try { //執行完執行await(),等待所有日誌讀取完畢 logger.info("檔案:"+filePath+" 讀取完畢"); cb.await(); } catch (Exception e) { e.printStackTrace(); } } //遞迴遍歷某資料夾下的所有檔案 private void readfile(String filepath)throws FileNotFoundException, IOException { File file = new File(filepath); if (!file.isDirectory()) { serviceOrderLogService.parseFileContent(file,null,false); file.delete(); }else if(file.isDirectory()){//目錄 String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) {//檔案,讀取內容後刪除 System.out.println("讀取檔案:"+readfile.getPath()); serviceOrderLogService.parseFileContent(readfile,null,false); readfile.delete(); }else if (readfile.isDirectory()){//目錄 readfile(filepath + "\\" + filelist[i]); } } } } } //所有日誌檔案讀取完畢,列印結果,最後執行 @Override public void run() { Set<String> set = map.keySet(); for(String s:set){ System.out.println("最終結果:"+map.get(s)); } } }