簡單的多執行緒複製檔案

一個浪子而已發表於2020-11-26

 Demo.class

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

public class demo {
    private static String sourceRootPath = "D:\\source-photo\\";
    private static String toRootPath = "D:\\compressed-photo\\";
    private static AtomicInteger count = new AtomicInteger(2);
    private static AtomicInteger index = new AtomicInteger(0);
    private static final int workSize = 8;

    public static void main(String[] args) {
        /*try {
            copyFiles(1000);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        try {
            compressPhotoFiles();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void compressPhotoFiles() throws InterruptedException {
        // TODO 檢查路徑是否合法
        // 這裡需要修改為返回檔案路徑,返回檔案會造成記憶體溢位
        List<File> list = new ArrayList<>(FileUtils.listFiles(new File(sourceRootPath),new String[]{"png","jpg"},true));
        Calendar start = Calendar.getInstance();
        ExecutorService executor = Executors.newFixedThreadPool(workSize);
        for(int i=0;i<=workSize;i++){
            executor.submit(new CompressPhotoRunnable(list,toRootPath,index));
        }
        executor.shutdown();
        while(true){
            if (executor.isTerminated()) {
                double minute = (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis())/1000/60.0;
                System.out.println("圖片壓縮完成:"+minute+"分鐘");
                break;
            }
            Thread.currentThread().sleep(1000);
        }
    }


    public static void copyFiles(int size) throws ExecutionException, InterruptedException {
        Calendar start = Calendar.getInstance();
        ExecutorService executor = Executors.newFixedThreadPool(workSize);
        ArrayList<Boolean> list = new ArrayList<>();
        for (int i=1;i<=workSize;i++){
            Future<Boolean> isOk = executor.submit(new CopyFileCallable(sourceRootPath,i,count,size));
            list.add((Boolean) isOk.get());
        }
        System.out.println(list.toString());
        executor.shutdown();
        while(true){
            if (executor.isTerminated()) {
                double minute = (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis())/1000/60.0;
                System.out.println("圖片複製完成:"+minute+"分鐘");
                break;
            }
            Thread.currentThread().sleep(1000);
        }
    }
}

執行緒類CopyFileRunnable,建立大量圖片用於測試

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;

public class CopyFileCallable implements Callable {
    private String fromPath;
    private Integer index ;
    private AtomicInteger count;
    private static Integer size;

    public CopyFileCallable(String fromPath,Integer index,AtomicInteger count,Integer size){
        this.fromPath = fromPath;
        this.index = index;
        this.count = count;
        this.size = size;

    }

    @Override
    public Boolean call() throws Exception {
        String fromPath = this.fromPath + index + ".jpg";
        int addCount = 0;
        File from = new File(fromPath);
        while(!from.exists()){
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            from = new File(fromPath);
        }

        while((addCount=count.getAndAdd(1))<=size && from.canRead()){
            System.out.println(addCount);

            String toPath = fromPath + addCount + ".jpg";
            File to = new File(toPath);
            try {
                if(!to.exists()){
                    Files.copy(from.toPath(),to.toPath());
                    // System.out.println("圖片執行緒"+i+"-"+(addCount+1)+" 已建立");
                }
            } catch (IOException e) {
                // e.printStackTrace();
                System.out.println("檔案"+addCount+".jpg 複製失敗:"+e.getMessage());
            }
            to = null;
        }
        return true;
    }
}

壓縮圖片執行緒類

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class CompressPhotoRunnable implements Runnable{
    private List<File> files;
    private String toPath;
    private AtomicInteger index;

    public CompressPhotoRunnable(List<File> files, String toPath, AtomicInteger index) {
        this.files = files;
        this.toPath = toPath;
        this.index = index;
    }

    @Override
    public void run() {
        int nowIndex = 0;
        File file;
        double scale = 1;
        int min;
        while ((nowIndex=index.getAndAdd(1))<=files.size()){
            System.out.println(nowIndex);
            try {
                file = files.get(nowIndex);
                BufferedImage buff = ImageIO.read(file);
                if((min = Math.min(buff.getHeight(),buff.getWidth()))>500){
                    // 小的邊根據比例壓縮到500畫素
                    scale = Math.round((500.00/min*100))/100.00;
                }
                Thumbnails.of(buff).scale(scale).toFile(toPath+file.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

 

相關文章