From e807603f3a63ba40f212dd765abf425950cf4b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=9D=E4=B8=8D=E7=AD=89=E7=90=B4=E7=94=9F?= <39022409+zhicheng-ning@users.noreply.github.com> Date: Thu, 20 Apr 2023 19:32:39 +0800 Subject: [PATCH] refactor: fix-thread-executor (#38) --- .../cn/nzcer/odapi/cron/SyncDataBase.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/nzcer/odapi/cron/SyncDataBase.java b/src/main/java/cn/nzcer/odapi/cron/SyncDataBase.java index ad038c0..520b3e6 100644 --- a/src/main/java/cn/nzcer/odapi/cron/SyncDataBase.java +++ b/src/main/java/cn/nzcer/odapi/cron/SyncDataBase.java @@ -139,11 +139,16 @@ public Set parseRepo(String url, int count) throws IOException { // 每月 3 日凌晨 1 点启动定时任务更新仓库的所有指标数据 @Scheduled(cron = "0 0 1 3 * ?") public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, InterruptedException { + // RepoMetrics 线程池 + ThreadPoolExecutor executor = new ThreadPoolExecutor(20, + 100, + 10, + TimeUnit.MICROSECONDS, + new LinkedBlockingQueue()); log.info("Repo Metrics 定时任务启动:" + new Date()); log.info("清空 repo_metric 表"); repoMetricService.truncateRepoMetric(); Set allRepo = getAllRepo(); - log.info("获取到的 repo 数量为: " + allRepo.size()); List tokens = getTokens(); List errorRepos = new ArrayList<>(); CountDownLatch countDownLatch = new CountDownLatch(allRepo.size()); @@ -171,15 +176,10 @@ public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, I writer.write(str + System.lineSeparator()); } writer.close(); + executor.shutdown(); } - // 自定义线程池 - private static ThreadPoolExecutor executor = new ThreadPoolExecutor(20, - 100, - 10, - TimeUnit.MICROSECONDS, - new LinkedBlockingQueue()); /** * 插入所有仓库的 star 和 fork 数据 @@ -188,11 +188,16 @@ public void insertAllRepoMetrics() throws IOException, BrokenBarrierException, I */ @Scheduled(cron = "0 0 5 * * ?") public void insertAllRepoStarAndFork() throws InterruptedException, BrokenBarrierException { + // RepoStarAndFork 线程池 + ThreadPoolExecutor executor = new ThreadPoolExecutor(20, + 100, + 10, + TimeUnit.MICROSECONDS, + new LinkedBlockingQueue()); log.info("Repo Statistic 定时任务启动:" + new Date()); log.info("清空 repo_statistic 表"); repoStatisticService.truncateRepoStatistic(); List> repoInfo = repoMetricService.getRepoInfo(); - log.info("仓库的数量: " + String.valueOf(repoInfo.size())); List tokens = getTokens(); CountDownLatch countDownLatch = new CountDownLatch(repoInfo.size()); int totalRepo = 0; @@ -214,6 +219,7 @@ public void insertAllRepoStarAndFork() throws InterruptedException, BrokenBarrie log.info("仓库的数量: " + String.valueOf(repoInfo.size())); log.info("已执行完的仓库数量:" + executor.getCompletedTaskCount()); log.info("所有子线程执行完毕"); + executor.shutdown(); } /**