File Compression Using Java Multithreading

single-threaded test

@SneakyThrows
private void singleThreadPrint() {
    
    
    // 计算运行时间
    long start = System.currentTimeMillis();

    // 使用多线程将数组数组组装到zip文件
    ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(Paths.get("F:\\test1.zip")));

    for (int i = 0; i < 999; i++) {
    
    
        ZipEntry zipEntry = new ZipEntry(i + "test.txt");
            try {
    
    
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write(RandomUtil.randomString(999999).getBytes());
                zipOutputStream.closeEntry();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
    }
    zipOutputStream.close();

    // 计算运行时间
    long end = System.currentTimeMillis();
    System.out.println("运行时间:" + (end - start) + "ms");
}

Test result: 40495ms

multithreaded test

Introduce dependencies

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>
@SneakyThrows
public void multhreadPrint(){
    
    
    // 计算运行时间
    long start = System.currentTimeMillis();

	// 测试机为16核
    ExecutorService executor = new ThreadPoolExecutor(16, 32, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(50), new MyRejectedExecutionHandler());
    ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
    OutputStream outputStream = Files.newOutputStream(Paths.get("F:\\test2.zip"));
    ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
    zipArchiveOutputStream.setEncoding("UTF-8");
    for (int i = 0; i < 999; i++) {
    
    
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(RandomUtil.randomString(999999).getBytes());
        final InputStreamSupplier inputStreamSupplier = () -> {
    
    
            try {
    
    
                return byteArrayInputStream;
            } catch (Exception e) {
    
    
                e.printStackTrace();
                return new NullInputStream(0);
            }
        };
        ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry( i + "test.txt");
        zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
        zipArchiveEntry.setSize(byteArrayInputStream.available());
        zipArchiveEntry.setUnixMode(UnixStat.FILE_FLAG | 436);
        parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
    }
    parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
    zipArchiveOutputStream.close();
    outputStream.close();

    // 计算运行时间
    long end = System.currentTimeMillis();
    System.out.println("运行时间:" + (end - start) + "ms");
}

public static class MyRejectedExecutionHandler implements RejectedExecutionHandler {
    
    

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    
    
        try {
    
    
            //阻塞
            executor.getQueue().put(r);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}

Test result: 7481ms

Conclusion: Multi-threaded execution time is 1/4 of single-threaded

Guess you like

Origin blog.csdn.net/ChinaLiaoTian/article/details/128822864