使用多线程(线程池ThreadPoolExecutor读取多个文件、计算多个文件总行数)小demo
时间:2023-12-04 02:37:02
包括 ThreadPoolExecutor、CountDownLatch、Callable、RandomAccessFile、Nio
1.用多线程读取多个文件。
2.用多线程获取多个文件的行数后,计算几个文件的总行数。
import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.file.Files; import java.nio.file.Paths; import java.util.concurrent.*; public class Test { public static void main(String[] args) throws Exception { long startTime = System.currentTimeMillis(); final int thNum = 4; final String[] filePath = { "D:\\zax\\1.txt", "D:\\zax\\2.txt", "D:\\zax\\1.txt", "D:\\zax\\2.txt", }; CountDownLatch doneSignal = new CountDownLatch(thNum); ThreadPoolExecutor executor = new ThreadPoolExecutor( 4, 4, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(4) ); // 存放总行数 long res = 0; for (int i = 0; i < 4; i ) { var rf = new ReadFileThread1(doneSignal, filePath[i]); var rf2 = new ReadFileThread2(doneSignal, filePath[i]); // 获取并 计算总行数 Future submit = executor.submit(rf); res = submit.get(); // 批量读取 executor.execute(rf2); // Future> submit1 = executor.submit(rf2); } System.out.println(res); try { doneSignal.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { executor.shutdown(); } long endTime = System.currentTimeMillis(); System.out.println("The totally executed time: " (endTime - startTime)); } } /** * 多线程读取多个文件,获得总行数 */ class ReadFileThread1 implements Callable { private CountDownLatch doneSignal; private String path; public ReadFileThread1(CountDownLatch doneSignal, String path){ this.doneSignal = doneSignal; this.path = path; } @Override public Long call() throws Exception { long count = Files.lines(Paths.get(new File(path).getPath())).count(); doneSignal.countDown(); return count; } } /** * 多线程读取多个文件 */ class ReadFileThread2 extends Thread{ private RandomAccessFile raf; private CountDownLatch doneSignal; private final int bufLen = 256; private String path; public ReadFileThread2(CountDownLatch doneSignal, String path){ this.doneSignal = doneSignal; this.path = path; } @Override public void run() { long start = System.currentTimeMillis(); try { raf = new RandomAccessFile(path, "rw"); raf.seek(0); long contentLen = new File(path).length(); long times = contentLen / bufLen 1; byte[] buff = new byte[bufLen]; int hasRead = 0; String result = null; for (int i=0; i < times; i ) { hasRead = raf.read(buff); if(hasRead < 0){ break; } result = new String(buff, "gb2312"); } System.out.println(Thread.currentThread() " result = " result); doneSignal.countDown(); } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println(getName() " " path " total Time: " (end - start)); } }