简体   繁体   English

并行处理Windows批处理文件

[英]Parallel processing Windows batch file

I have a pretty big number of files that need to be converted to a different format. 我有大量文件需要转换为其他格式。 The converting is done via a Java-JAR-File that gets takes the filename as a parameter. 转换是通过Java-JAR-File完成的,该文件将文件名作为参数。 I now have a Windows batchfile that uses a for loop to loop through all the files (there is a file that contains a list of all files that need to be converted) 我现在有一个Windows批处理文件,该文件使用for循环遍历所有文件(有一个文件包含需要转换的所有文件的列表)

for /F %%i in (all_files.txt) do call java -cp %Classpath% de.xyz.Convert -xml %%i .\xml

Now the machine I want to do this on has eight cores. 现在,我要在其上执行此操作的计算机具有八个内核。 The number of files is about 360.000 and I would like it to take as little time as possible, so I'd like to use as many cores as possible. 文件数量约为360.000,我希望它花费的时间尽可能少,所以我想使用尽可能多的内核。 How would I go about using multiple cores as easy as possible? 我将如何尽可能容易地使用多个内核? Is Windows going to be doing that on its own? Windows会自己做吗?

Ok, because I hadn't actually done it before, I knocked this up. 好的,因为我之前从未真正做过,所以我把它敲了。 It's not great, and the lib I used was a jar I created to crash after 2 mins.. Hopefully you'll be able to reverse engineer this for your needs. 这不是很好,我使用的lib是2分钟后崩溃的我创建的jar。希望您可以根据需要对它进行反向工程。

package test;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class Test {
    public static void main(String[] args) throws InterruptedException, IOException {
        BlockingQueue<Runnable> runnableQueue = new LinkedBlockingQueue<>();
        ExecutorService executorServ = new ThreadPoolExecutor(8, 8, 1, TimeUnit.MINUTES, runnableQueue);
        runnableQueue.add(new RunCrash("Example")); // Add one for each file...
        executorServ.shutdown();
        while(!executorServ.isTerminated()) {
            // running
        }
    }
}

class RunCrash implements Runnable {

    private String fileName;
    RunCrash(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void run() {
        System.out.println(fileName);
        try {
            crash.CrashMe.main(new String[]{fileName});
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Oh, you can let the main thread die before the others finish, I believe the JVM will keep the executor and associated queue. 哦,您可以让主线程在其他线程完成之前就死掉,我相信JVM将保留执行程序和关联的队列。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM