以下任何代码片段都可以保证pool1仅在池执行完之后才能启动吗?
代码段1
ExecutorService pool = Executors.newFixedThreadPool(numberOfThreads);
for (FObject action : list) {
if (action instanceof FObject) {
Runnable run = new ImageProcessRunnable(action);
pool.execute(run);
}
}
pool.shutdown();
if (pool.awaitTermination(10, TimeUnit.SECONDS)) {
pool.shutdownNow();
}
ExecutorService pool1 = Executors.newFixedThreadPool(numberOfThreads);
synchronized (ImageHelper.getFilePaths()) {
for (String file :ImageHelper.getFilePaths()) {
pool1.execute(() -> {
UploadFileAsAppUser.uploadFileAsBoxAppUser(file, boxLocation);
});
}
}
pool1.shutdown();
if (pool1.awaitTermination(10, TimeUnit.SECONDS)) {
pool1.shutdownNow();
}
代码段2
for (FObject action : list) {
if (action instanceof FObject) {
ImageProcessor processor = new ImageProcessor();
}
}
ExecutorService pool1 = Executors.newFixedThreadPool(numberOfThreads);
synchronized (ImageHelper.getFilePaths()) {
for (String file :ImageHelper.getFilePaths()) {
pool1.execute(() -> {
UploadFileAsAppUser.uploadFileAsBoxAppUser(file, boxLocation);
});
}
}
pool1.shutdown();
if (pool1.awaitTermination(10, TimeUnit.SECONDS)) {
pool1.shutdownNow();
}
在代码段1中,我正在处理以及在多个线程中上传图像。在代码段2中,我正在单线程中处理图像,并以多个线程上传它们。