如何在java中将进程构建器命令作为线程运行

时间:2014-08-07 05:16:23

标签: java multithreading imagemagick image-conversion

我需要在图像magick工具的帮助下使用流程构建器在文件夹中进行图像转换。我需要在转换后删除原始文件,但是当我使用等待它需要很长时间才能完成。我使用了以下代码

          for (int h = 0; h < convimagefolder.size(); h++) {
            /* CMYK conversion for jpg and tiff and also .psd conversion*/
            destpathfinalconvs = new File(tempdir + "/" + convimagefolder.get(h));

            //  JOptionPane.showMessageDialog(null, "CMYK jpg conversion - folder assign");
            ProcessBuilder pb = new ProcessBuilder("mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.jpg");
            pb.redirectErrorStream(true);
            try {
                Process p = pb.start();
                System.out.println("CMYK to RGB jpg done for "+convimagefolder.get(h));
//                try {
//                    p.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
            }

            //  JOptionPane.showMessageDialog(null, "tif conversion - folder assign");
            ProcessBuilder pb1 = new ProcessBuilder("mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.tif");
            pb1.redirectErrorStream(true);
            try {
                Process p1 = pb1.start();
                System.out.println("cmyk tif to rgb done " + convimagefolder.get(h));
//                try {
//                    p1.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
            }

            //  JOptionPane.showMessageDialog(null, "psd conversion - folder assign");
            ProcessBuilder pb2 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.psd[0]");
            pb2.redirectErrorStream(true);
            try {
                Process p2 = pb2.start();
                System.out.println("psd to jpg done" + convimagefolder.get(h));
//                try {
//                    p2.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
            }
            /* CMYK conversion for jpg and tiff and also .psd conversion*/


            /* tif to jpg conversion*/
            ProcessBuilder pb3 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.tif");
            pb3.redirectErrorStream(true);
            try {
                Process p3 = pb3.start();
//                try {
//                    p3.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
                System.out.println(".tif to jpg done successfully"+ convimagefolder.get(h));
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Error in tif to jpg conversion " + ex.toString());

            }


            /* pdf to jpg*/
            ProcessBuilder pb4 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.pdf");
            pb4.redirectErrorStream(true);
            try {
                Process p4 = pb4.start();
//                try {
//                    p4.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
                System.out.println(".pdf to jpg done successfully"+ convimagefolder.get(h));
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Error in tif to jpg conversion " + ex.toString());

            }


            /* image size reduction*/
            ProcessBuilder pb5 = new ProcessBuilder("mogrify", "-strip", "-quality", "50%", destpathfinalconvs.toString() + "\\" + "*.jpg");
            //   ProcessBuilder pb5 = new ProcessBuilder("mogrify", "-path ", destinationpath.toString(), "-strip", "-quality", "50%",  destinationpath.toString() + "\\" + "*.jpg");
            pb5.redirectErrorStream(true);
            try {
                Process p5 = pb5.start();
//                try {
//                    p5.waitFor();
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
//                }
                System.out.println("image conversion done successfully"+ convimagefolder.get(h));
            } catch (IOException ex) {
                Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Error in jpg compression " + ex.toString());
            }
        }
        JOptionPane.showMessageDialog(null, "Image compression and conversion done using Image Magick");

是否可以通过线程执行此操作。请建议缩短转换时间

1 个答案:

答案 0 :(得分:2)

我会看看Executors,这将允许您汇集一系列任务并让它们自动在一个或多个线程中执行...

例如......

ExecutorService service = Executors.newFixedThreadPool(7);

for (int h = 0; h < convimagefolder.size(); h++) {

    /* CMYK conversion for jpg and tiff and also .psd conversion*/
    destpathfinalconvs = new File(tempdir + "/" + convimagefolder.get(h));

    service.submit(new ProcessTask(
            "CMYK to RGB jpg done for " + convimagefolder.get(h),
            new String[]{
                "mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.jpg"}
    ));
    service.submit(new ProcessTask(
            "CMYK to RGB jpg done for " + convimagefolder.get(h),
            new String[]{
                "mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.jpg"}
    ));
    service.submit(new ProcessTask(
            "cmyk tif to rgb done " + convimagefolder.get(h),
            new String[]{
                "mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.tif"}
    ));
    service.submit(new ProcessTask(
            "psd to jpg done" + convimagefolder.get(h),
            new String[]{
                "mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.psd[0]"}
    ));
    service.submit(new ProcessTask(
            ".tif to jpg done successfully" + convimagefolder.get(h),
            new String[]{
                "mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.tif"
            }
    ));
    service.submit(new ProcessTask(
            ".pdf to jpg done successfully" + convimagefolder.get(h),
            new String[]{
                "mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.pdf"
            }
    ));
    service.submit(new ProcessTask(
            "image conversion done successfully" + convimagefolder.get(h),
            new String[]{
                "mogrify", "-strip", "-quality", "50%", destpathfinalconvs.toString() + "\\" + "*.jpg"}
    ));
}

实际Callable任务......

public class ProcessTask implements Callable<Integer> {

    private String cmds[];
    private String name;

    public ProcessTask(String name, String[] cmds) {
        this.cmds = cmds;
        this.name = name;
    }

    @Override
    public Integer call() throws Exception {
        System.out.println("Started " + name);
        ProcessBuilder pb = new ProcessBuilder(cmds);
        pb.redirectErrorStream(true);
        Process p5 = pb.start();
        p5.waitFor();
        System.out.println("Completed " + name);
        return p5.exitValue();
    }

}

现在,如果您想等到所有任务完成并检查其状态(查看谁成功完成以及谁失败了),您可以将所有Callable添加到List并使用ExecutorService#invokeAll(List),它会阻止,直到Callable中的所有List都已完成。然后,您可以遍历返回的List并查看是否有任何Callable失败(调用Callable#get将返回Callable返回的值或抛出{{} 1}} Exception提出......)

另一种方法可能是,不是在单个Callable内执行流程,而是创建一个Callable,其中引用Callable,将这些添加到File并使用引用的ExecutorService执行Callable中的每个步骤,等待每个步骤完成...

这样,当你走到尽头时,你可以删除该文件。这将允许您并行处理每个文件......