我试图对带有threds池的文件进行操作但没有运气,它非常慢......
这是我试过的:
int chunks = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newCachedThreadPool();
int iterations = listOfFiles.length;
if(chunks > iterations)
chunks = iterations;
for(int i = 0; i < chunks; i++){
final int from = iterations / chunks * i;
final int to = iterations / chunks * (i + 1);
service.submit(new Thread(){
@Override
public void run() {
for(int j = from; j < to; j++) {
File curFile = listOfFiles[j];
....
}
}
});
}
service.shutdown();
try {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {}
当:
iterations = 150,000;
chunks = 4;
实际上每组做4次。它看起来像:
file
file
file
file
------its stop for 2-3 seconds---
file
file
file
file
------its stop for 2-3 seconds---
file
file
file
file
------its stop for 2-3 seconds---
.......
如果不停止每组2-3秒,我怎么能更快地完成它?
tnx很多