From 8f9d7d5297a607926bb1ae6c07943a37e7fec5be Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 4 Jul 2022 11:03:10 +0200 Subject: [PATCH] run each task in a extra thread --- .../sysbackup/tasks/TaskBuilder.java | 10 +-- .../sysbackup/tasks/impl/FileSystemTask.java | 62 ++++++++----------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java index f204c77..d00038d 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java @@ -6,6 +6,7 @@ import net.horizoncode.sysbackup.config.Config; import net.horizoncode.sysbackup.tasks.impl.DatabaseTask; import net.horizoncode.sysbackup.tasks.impl.FileSystemTask; import net.horizoncode.sysbackup.tasks.impl.VacuumTask; +import net.horizoncode.sysbackup.threading.ThreadPool; import org.apache.commons.io.FilenameUtils; import org.tomlj.TomlArray; @@ -25,6 +26,7 @@ public class TaskBuilder { private final String taskName; @Builder.Default private final LinkedBlockingQueue taskList = new LinkedBlockingQueue<>(); + @Builder.Default @Getter private final ThreadPool threadPool = new ThreadPool(3, 10); private final File executionPath; @@ -74,7 +76,7 @@ public class TaskBuilder { new VacuumTask(backupDir, unit, value) { @Override public void onDone() { - executeNextTask(); + getThreadPool().getPool().submit(() -> executeNextTask()); } }); } @@ -92,7 +94,7 @@ public class TaskBuilder { new FileSystemTask(target, outputFile) { @Override public void onDone() { - executeNextTask(); + getThreadPool().getPool().submit(() -> executeNextTask()); } }); }); @@ -117,7 +119,7 @@ public class TaskBuilder { new DatabaseTask(databaseCredentials, outputFile) { @Override public void onDone() { - executeNextTask(); + getThreadPool().getPool().submit(() -> executeNextTask()); } }); } else { @@ -125,7 +127,7 @@ public class TaskBuilder { } } - executeNextTask(); + getThreadPool().getPool().submit(this::executeNextTask); } private void executeNextTask() { diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java b/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java index 9b26617..0bfc9c1 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java @@ -4,7 +4,6 @@ import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarStyle; import net.horizoncode.sysbackup.tasks.Task; -import net.horizoncode.sysbackup.threading.ThreadPool; import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.progress.ProgressMonitor; @@ -14,11 +13,9 @@ import java.nio.file.Paths; public class FileSystemTask extends Task { private final File target; - private final ThreadPool threadPool; private File outputZipFile; public FileSystemTask(String folderOrFilePath, File outputZipFile) { - this.threadPool = new ThreadPool(3, 10); this.target = Paths.get(folderOrFilePath).toFile(); if (!target.exists()) { onDone(); @@ -32,38 +29,33 @@ public class FileSystemTask extends Task { @Override public void start() { - threadPool - .getPool() - .submit( - () -> { - try (ZipFile zipFile = new ZipFile(outputZipFile)) { - System.out.println("Indexing files..."); - ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); - zipFile.setRunInThread(true); - zipFile.addFolder(target); - ProgressBarBuilder pbb = - new ProgressBarBuilder() - .setStyle(ProgressBarStyle.ASCII) - .setInitialMax(progressMonitor.getTotalWork()) - .setTaskName("Adding Files...") - .setUnit("MiB", 1048576); + try (ZipFile zipFile = new ZipFile(outputZipFile)) { + System.out.println("Indexing files..."); + ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); + zipFile.setRunInThread(true); + zipFile.addFolder(target); + ProgressBarBuilder pbb = + new ProgressBarBuilder() + .setStyle(ProgressBarStyle.ASCII) + .setInitialMax(progressMonitor.getTotalWork()) + .setTaskName("Adding Files...") + .setUnit("MiB", 1048576); - try (ProgressBar pb = pbb.build()) { - while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) { - pb.stepTo(progressMonitor.getWorkCompleted()); - Thread.sleep(100); - } - pb.stepTo(progressMonitor.getTotalWork()); - } catch (Exception exception) { - exception.printStackTrace(); - onDone(); - } - progressMonitor.endProgressMonitor(); - onDone(); - } catch (Exception ex) { - ex.printStackTrace(); - onDone(); - } - }); + try (ProgressBar pb = pbb.build()) { + while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) { + pb.stepTo(progressMonitor.getWorkCompleted()); + Thread.sleep(100); + } + pb.stepTo(progressMonitor.getTotalWork()); + } catch (Exception exception) { + exception.printStackTrace(); + onDone(); + } + progressMonitor.endProgressMonitor(); + onDone(); + } catch (Exception ex) { + ex.printStackTrace(); + onDone(); + } } }