簡體   English   中英

並行處理Windows批處理文件

[英]Parallel processing Windows batch file

我有大量文件需要轉換為其他格式。 轉換是通過Java-JAR-File完成的,該文件將文件名作為參數。 我現在有一個Windows批處理文件,該文件使用for循環遍歷所有文件(有一個文件包含需要轉換的所有文件的列表)

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

現在,我要在其上執行此操作的計算機具有八個內核。 文件數量約為360.000,我希望它花費的時間盡可能少,所以我想使用盡可能多的內核。 我將如何盡可能容易地使用多個內核? Windows會自己做嗎?

好的,因為我之前從未真正做過,所以我把它敲了。 這不是很好,我使用的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();
        }
    }
}

哦,您可以讓主線程在其他線程完成之前就死掉,我相信JVM將保留執行程序和關聯的隊列。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM