OSDN Git Service

fix: use thread pool to update manually
authorhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Wed, 29 Dec 2021 02:56:11 +0000 (11:56 +0900)
committerhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Wed, 29 Dec 2021 02:56:11 +0000 (11:56 +0900)
generated/database.json
src/com/ranfa/lib/ManualUpdateThreadImpl.java
src/com/ranfa/main/DelesteRandomSelector.java

index 7d54c8a..fccfe37 100644 (file)
   }, {
     "attribute" : "クール",
     "name" : "Drastic Melody",
-    "difficulty" : "MASTER+",
+    "difficulty" : "MASTER+",
     "level" : 29,
     "notes" : 926,
-    "albumType" : "Not-implemented"
+    "albumType" : "ALBUM B"
   }, {
     "attribute" : "キュート",
     "name" : "いとしーさー♥",
     "level" : 16,
     "notes" : 405,
     "albumType" : "Not-Implemented"
+  }, {
+    "attribute" : "クール",
+    "name" : "Drastic Melody",
+    "difficulty" : "MASTER+",
+    "level" : 29,
+    "notes" : 934,
+    "albumType" : "ALBUM B"
+  }, {
+    "attribute" : "全タイプ",
+    "name" : "夕映えプレゼント",
+    "difficulty" : "MASTER+",
+    "level" : 29,
+    "notes" : 887,
+    "albumType" : "ALBUM B"
   } ]
 }
\ No newline at end of file
index 8ff7efd..5fdf858 100644 (file)
@@ -2,7 +2,6 @@ package com.ranfa.lib;
 
 import java.util.ArrayList;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
 import java.util.function.BiConsumer;
@@ -45,12 +44,16 @@ public class ManualUpdateThreadImpl implements Runnable {
                this.logger.info("Checking database updates...");
                CompletableFuture<ArrayList<Song>> webData = CompletableFuture.supplyAsync(Scraping::getWholeData, this.executor);
                CompletableFuture<ArrayList<Song>> localData = CompletableFuture.supplyAsync(Scraping::getFromJson, this.executor);
-               try {
-                       this.updateConsumer.accept(webData.get(), localData.get());
-               } catch (InterruptedException | ExecutionException e) {
-                       this.logger.warn("Update failed.", e);
-               }
-               flag = true;
+               CompletableFuture<Void> afterUpdateFuture = webData.thenAcceptBothAsync(localData, this.updateConsumer, this.executor);
+               afterUpdateFuture.whenCompleteAsync((ret, e) -> {
+                       if(e != null) {
+                               this.logger.warn("Manual update process has been ended with exception.", e);
+                       } else {
+                               this.logger.info("Manual update process has been ended successfully.");
+                               flag = true;
+                       }
+
+               }, this.executor);
        }
 
        public boolean getFlag() {
index 613190c..f2884ff 100644 (file)
@@ -154,7 +154,7 @@ public class DelesteRandomSelector extends JFrame {
                };
                Runnable setEnabled = () -> {
                        try {
-                               Thread.sleep(1000);
+                               Thread.sleep(3 * 1000L);
                        } catch (InterruptedException e1) {
                                this.logger.error("Thread has been interrupted during waiting cooldown.", e1);
                        }
@@ -368,10 +368,7 @@ public class DelesteRandomSelector extends JFrame {
                this.btnManualUpdate = new JButton(Messages.MSGManualUpdate.toString());
                this.btnManualUpdate.addActionListener(e -> {
                        this.impl = new ManualUpdateThreadImpl();
-                       this.manualUpdateThread = new Thread(this.impl);
-                       this.manualUpdateThread.setName("ManualUpdate-thread");
-                       this.manualUpdateThread.setDaemon(false);
-                       this.manualUpdateThread.start();
+                       es.submit(this.impl);
                });
                this.panelEast.add(this.btnManualUpdate, "1, 9");
                this.panelEast.add(this.btnTwitterIntegration, "1, 11");
@@ -379,7 +376,9 @@ public class DelesteRandomSelector extends JFrame {
                this.btnExit = new JButton(Messages.MSGTerminate.toString());
                this.btnExit.addActionListener(e -> {
                        if(DelesteRandomSelector.this.softwareUpdateFuture.isDone() || DelesteRandomSelector.this.albumTypeEstimateFuture.isDone() || !this.impl.getFlag()) {
-                               DelesteRandomSelector.this.logger.info("Requested Exit by Button");
+                               DelesteRandomSelector.this.logger.info("Requested Exit by Button.");
+                               this.logger.info("Shut down thread pool.");
+                               es.shutdown();
                                System.exit(0);
                        } else {
                                JOptionPane.showMessageDialog(null, Messages.MSGInternalYpdateNotDoneYet.toString());