mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Implement multi threading to improve throughput
This commit is contained in:
@@ -5,41 +5,57 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigProcessor;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl.MkvFileCollector;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Log4j2
|
||||
public class AttributeUpdaterKernel {
|
||||
MkvFileCollector collector = new MkvFileCollector();
|
||||
int filesChangedAmount = 0;
|
||||
int filesNotChangedAmount = 0;
|
||||
long runtime = 0;
|
||||
|
||||
@SneakyThrows
|
||||
public void execute(String path) {
|
||||
List<AttributeConfig> configPattern = ConfigUtil.loadConfig();
|
||||
List<File> allValidPaths = collector.loadFiles(path);
|
||||
ExecutorService executor = Executors.newFixedThreadPool(ConfigUtil.getThreadCount());
|
||||
|
||||
long beforeTimer = System.currentTimeMillis();
|
||||
if(allValidPaths != null && configPattern != null){
|
||||
for(File file : allValidPaths){
|
||||
List<FileAttribute> attributes = collector.loadAttributes(file);
|
||||
if (attributes.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
boolean fileHasChanged = false;
|
||||
for(AttributeConfig config : configPattern){
|
||||
/*
|
||||
* Creating new ArrayList, because the method removes elements from the list by reference
|
||||
*/
|
||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
|
||||
if(fileHasChanged){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(! fileHasChanged){
|
||||
log.info(file.getName() + " didn't change!");
|
||||
}
|
||||
}
|
||||
System.out.print("Running");
|
||||
allValidPaths.forEach(file -> executor.submit(() -> process(configPattern, file)));
|
||||
}else{
|
||||
log.error("Path is not valid or config has errors!");
|
||||
}
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||
runtime = System.currentTimeMillis() - beforeTimer;
|
||||
|
||||
System.out.printf("%nFiles changed: %s%n", filesChangedAmount);
|
||||
System.out.printf("Files not changed: %s%n", filesNotChangedAmount);
|
||||
System.out.printf("Runtime: %ss%n", runtime / 1000);
|
||||
}
|
||||
|
||||
private void process(List<AttributeConfig> configPattern, File file) {
|
||||
List<FileAttribute> attributes = collector.loadAttributes(file);
|
||||
boolean fileHasChanged = false;
|
||||
|
||||
if (attributes.isEmpty()) return;
|
||||
for(AttributeConfig config : configPattern){
|
||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, attributes);
|
||||
if(fileHasChanged) break;
|
||||
}
|
||||
if(!fileHasChanged){
|
||||
log.info("File didn't change: {}", file.getName());
|
||||
filesNotChangedAmount++;
|
||||
} else {
|
||||
filesChangedAmount++;
|
||||
}
|
||||
System.out.print(".");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user