From e490487c6363988228ade17a512a1484d7807d8a Mon Sep 17 00:00:00 2001 From: jchrist Date: Fri, 15 Dec 2023 11:50:19 +0200 Subject: [PATCH] fix(threads): Cap updating threads Submit each repository update runnable to an executor service with fixed number of threads. This allows having a maximum cap number of threads for projects with 10s or 20s repos. Closes #55 Signed-off-by: jchrist --- .../gr/jchrist/gitextender/GitExtenderUpdateAll.java | 10 +++++----- .../gitextender/UpdateExecutingBackgroundTask.java | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java b/src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java index 658f98a..1592af8 100644 --- a/src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java +++ b/src/main/java/gr/jchrist/gitextender/GitExtenderUpdateAll.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @@ -122,13 +123,12 @@ private void updateRepositories(@NotNull Project project, @NotNull List - new Thread(new BackgroundableRepoUpdateTask(repo, - VcsImplUtil.getShortVcsRootName(repo.getProject(), repo.getRoot()), - settings, updateCountDown)) - .start()); + es.submit(new BackgroundableRepoUpdateTask(repo, VcsImplUtil.getShortVcsRootName(repo.getProject(), repo.getRoot()), + settings, updateCountDown))); UpdateExecutingBackgroundTask ubt = new UpdateExecutingBackgroundTask(project, "GitExtender Update All Repos", - accessToken, updateCountDown, executingFlag); + accessToken, updateCountDown, es, executingFlag); //ubt.queue(); new Thread(ubt::queue).start(); } diff --git a/src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java b/src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java index 7adb619..aa06961 100644 --- a/src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java +++ b/src/main/java/gr/jchrist/gitextender/UpdateExecutingBackgroundTask.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -16,14 +17,16 @@ public class UpdateExecutingBackgroundTask extends Task.Backgroundable { private static final Logger logger = Logger.getInstance(UpdateExecutingBackgroundTask.class); protected AccessToken accessToken; protected CountDownLatch updateCountDownLatch; + protected ExecutorService executor; protected AtomicBoolean executingFlag; public UpdateExecutingBackgroundTask( Project project, String title, - AccessToken accessToken, CountDownLatch updatingLatch, AtomicBoolean executing) { + AccessToken accessToken, CountDownLatch updatingLatch, ExecutorService executor, AtomicBoolean executing) { super(project, title, false, PerformInBackgroundOption.ALWAYS_BACKGROUND); this.accessToken = accessToken; this.updateCountDownLatch = updatingLatch; + this.executor = executor; this.executingFlag = executing; } @@ -36,6 +39,11 @@ public void run(@NotNull ProgressIndicator indicator) { } catch (Exception e) { logger.warn("error awaiting update latch!", e); } + try { + executor.shutdownNow(); + } catch (Exception e) { + logger.warn("error shutting down executor", e); + } logger.debug("update finished - " + Thread.currentThread().getName()); //the last task finished should clean up, release project changes and show info notification accessToken.finish();