From b7f0794bc477e1a74dc8b1d998dc399f354b4869 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Thu, 30 Sep 2021 20:47:43 +0100 Subject: [PATCH] Downloader: keep track of download queue. --- .../java/moe/nekojimi/chords/Downloader.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/moe/nekojimi/chords/Downloader.java b/src/main/java/moe/nekojimi/chords/Downloader.java index cf1ad3d..9f5c0a5 100644 --- a/src/main/java/moe/nekojimi/chords/Downloader.java +++ b/src/main/java/moe/nekojimi/chords/Downloader.java @@ -8,6 +8,9 @@ package moe.nekojimi.chords; import java.io.File; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -24,9 +27,9 @@ import java.util.regex.Pattern; */ public class Downloader implements Consumer { -// private final Queue downloadQueue = new LinkedBlockingDeque<>(); + private final List downloadQueue = new LinkedList<>(); private final LinkedBlockingDeque workQueue = new LinkedBlockingDeque<>(); - private final ThreadPoolExecutor exec = new ThreadPoolExecutor(1, 4, 30, TimeUnit.SECONDS, workQueue); + private final ThreadPoolExecutor exec = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, workQueue); private Consumer next; private BiConsumer messageHandler; @@ -38,9 +41,14 @@ public class Downloader implements Consumer @Override public void accept(Song song) { - exec.submit(() -> + downloadQueue.add(song); + exec.submit(new Runnable() { - download(song); + @Override + public void run() + { + download(song); + } }); } @@ -76,12 +84,14 @@ public class Downloader implements Consumer if (next != null) next.accept(song); + downloadQueue.remove(song); messageHandler.accept(song, null); } catch (Exception ex) { Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex); if (messageHandler != null) messageHandler.accept(song, ex); + downloadQueue.remove(song); } } @@ -95,4 +105,9 @@ public class Downloader implements Consumer this.messageHandler = messageHandler; } + public List getDownloadQueue() + { + return downloadQueue; + } + }