1
我有一個要求,我需要平行處理多個文件,我已經完成了下面,它的工作正常,但我怎樣才能使它更優化,期待任何建議。並行處理文件
long start = System.currentTimeMillis();
File [] files=new File("C:\\ftp").listFiles();
//System.out.println(files.length);
ExecutorService executor=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// System.out.println("available processors.."+Runtime.getRuntime().availableProcessors());
if(files.length>=1)
{
for(int i = 0;i<files.length; i++){
System.out.println("reading..."+i);
Runnable workerThread = new FileWorkerThread(files[i]);
executor.execute(workerThread);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
//然後我有一個run(),這是基本的讀/寫的files.Is這是一個好辦法,有什麼我可以做,使之更好。
關閉後使用'executor.awaitTermination(...)'而不是繁忙的循環。 – Pyranja
更好的是什麼? –
作爲最後一次通話,您有一個忙碌的等待,這是非常糟糕的做法。按照@Pyranja的建議使用'executor.awaitTermination(...)'。 –